Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts

Sunday, July 26, 2015

Convert WordPress Menu to Bootstrap Navigation Menu

WordPress is using for blogging and simple information website, Now every website required responsive for comparable all devices (Mobile, Tablet, Desktop) for that every website, create as responsive site. From that Bootstrap is most known as HTML, CSS and JavaScript platform for developing responsive website. So there are many WordPress free and paid theme available based on bootstrap platform.
When you create your own theme in WordPress using Bootstrap platform that most conman problems is converting WordPress menu to Bootstrap navigation menu. Here display solution for converting WordPress menu to Bootstrap navigation menu without using any extra script or function. Convert WordPress menu to Bootstrap using jQuery.
For download and use Bootstrap see main website http://getbootstrap.com/
Integrate Bootstrap into WordPress website add below script and CSS in header.php file.

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

Here enter CSS and script path according to Bootstrap path in your theme.
Now create any WordPress menu from admin panel Appearance->Menu.Save that menu with menu name.Now display menu anywhere in website using `wp_nav_menu` function as below.

wp_nav_menu(array('menu'=>'Main menu','menu_class'=>'nav navbar-nav','container'=>''));

Now convert menu to Bootstrap using jQuery.for that add latest jQuery script if not use in theme as below.(In WordPress already define jQuery in `wp_head()` function)
Now add below script for menu.

<script>
jQuery(document).ready(function(){
    jQuery("#menu-header-menu li").each(function(){
     if(jQuery(this).children('ul').length)
  {
   jQuery(this).addClass('dropdown');
   jQuery(this).children('a').addClass('dropdown-toggle');
   jQuery(this).children('a').attr('data-toggle','dropdown');
   jQuery(this).children('ul').addClass('dropdown-menu');
  }
    });
    //This jQuery use for a link redirection in Bootstrap navigation.
    jQuery('ul.nav > li a').click(function(e){
 window.location = jQuery(this).attr("href");
    });
});
</script>
 
Now check website in browser WordPress menu convert into Bootstrap navigation menu.This script use only first level of WordPress menu not use for WordPress M=multi level menu.

Saturday, June 27, 2015

Add or Remove Table Rows Using jQuery

When you create any dynamic HTML website than in website must save records. For that you use HTML tables for display records from database or from some array variable. Sometimes in table row data should be added dynamically by input text without no limit and not sure about how much row add or remove. That time you can use buttons to add and remove rows. For that see below code to add and remove table rows using jQuery.
For implement that code or integrate to any table first create HTML table with basic table tags.

<table cellpadding="1" id="tabledata" cellspacing="1" width="100%" border="1">
<thead>
<tr>
    <th>NO</th>
    <th>NAME</th>
    <th>EMAIL</th>
    <th>REMOVE</th>
</tr>
</thead>
<tbody>
<tr>
    <td align="center">1</td>
    <td align="center"><input type="text" /></td>
    <td align="center"><input type="email" /></td>
    <td align="center"><input type="button" class="removebutton" value="REMOVE" /></td>
</tr>
</tbody>
<tfoot>
<tr>
    <td colspan="4" align="right"><input type="button" class="addnewbutton" value="ADD NEW" /></td>
</tr>
</tfoot>
</table>
 
Now you want to set add and remove row data by click on `Add New` and `Remove` button, then use jQuery for that put live latest jQuery link from https://jquery.com/download on your page as below.

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
 

Add new table row using jQuery

For an added new row with name and email id input text when click on add a new button on above table, then adds jQuery event as script on `Add New` button click. for that assign class `addnewbutton` for that button as above table and script as below.

<script>
$(document).on('click', '.addnewbutton', function (){ //Add new row below Add New button row
$(this).closest('tr').before('<tr>'+
    '<td align="center">1</td>'+
    '<td align="center"><input type="text" /></td>'+
    '<td align="center"><input type="email" /></td>'+
    '<td align="center"><input type="button" class="removebutton" value="REMOVE" /></td>'+
    '</tr>');
     return false;
 });
</script>

Add row with number

Here for adding new row you want to also add an auto increment number for count total records than using jQuery find total table rows and then add an extra row of that table according to the next increment number. For that add `tabledata`as table id as above table and script as below.

<script>
$(document).on('click', '.addnewbutton', function (){
var totaltr = ($("#tabledata").find('tr').length) - 2; //For calculation of data row after remove table head and footer
$(this).closest('tr').before('<tr>'+
    '<td align="center">'+(totaltr + 1)+'</td>'+ //Add next increment value for new row
    '<td align="center"><input type="text" /></td>'+
    '<td align="center"><input type="email" /></td>'+
    '<td align="center"><input type="button" class="removebutton" value="REMOVE" /></td>'+
    '</tr>');
     return false;
 });
</script>
 

Remove table row using jQuery

For removing any row and its data when click on remove button in a table than add jQuery remove function use. For that create new class `removebutton` for each new Remove button and remove table row script as below.

<script>
  $(document).on('click', '.removebutton', function (){
      $(this).closest('tr').remove();
 });
</script>

Saturday, May 9, 2015

Custom integration of Google Translate in website

If you are developer or site owner and want to create multi language website using google translate than must add google translate toolbar easily from your google account Website Translator.Google provide you source code for your website just copy and past it to your website according to your setting that you set while create translate toolbar from Google Account.See complete steps.
When you want to add google translate with custom option like as check box,radio button,drop down or listing event for change language than need to old translate code as below.
Add translate script at head of website
 
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit" type="text/javascript">
 
Set new translate element for define translate languages.
 
<script>
function googleTranslateElementInit() {
      new google.translate.TranslateElement({ includedLanguages: 'en,ar,hi,fr', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}
</script>

Here in  'includedLanguages' set all languages code that you want to translate here include only 4 languages.
Create list for change language with onclick event.


<li><a onclick="return ChnageLang('ar')" title="ar">العربية -  Arabic</a></li>
<li><a onclick="return ChnageLang('')" title="ar">English  -  English</a></li>
<li><a onclick="return ChnageLang('fr')" title="ar">Français  -  French</a></li>
<li><a onclick="return ChnageLang('hi')" title="ar">हिंदी  -  Hindi</a></li> 
 
Here create new 'ChnageLang' event when click on below list for change language.
 
<script>
      function ChnageLang(value){
       createCookie('googtrans','/auto/'+value,1,'');
      }
      function createCookie(name, value, days, domain) {
         var date = new Date();
         date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
         var expires = "; expires=" + date.toGMTString();
         document.cookie = name + "=" + value + expires + "; domain=" + domain + "; path=/"';
      } 
</script>