javascript Clone

Using  javascript can make html clone on run time. Here we will see how to fomr a clone in the <table>’s  attributes <tr> and <td>values.

While making clone on the table attribute, here some html tag also present for the neatful presentation.

Create a table with id=”mainTbl”  as seen below with the simple html elements,

<table width=”100″ border=”0″ cellspacing=”0″ cellpadding=”2″ id=”mainTbl”>
             <tr>
               <td><select name=”reminder”>
                            <option value=”email” selected>Email</option>
                           </select> 
            </td>
                  <td>
                    <img src=”images/add.png” onclick=”insertRow();” /></td>
             </tr>
           </table>

Here onlick Add images as seen in the code we are calling insertRow() function to create clone as ,

function insertRow(){
    var tbl = document.getElementById(‘mainTbl’);   
     var lastRow = tbl.rows.length ;
    // if there’s no header row in the table, then iteration = lastRow + 1   
   var tr =document.createElement(“tr”);
   var td1 =document.createElement(“td”);
   var td2 = document.createElement(“td”);
   var td3 = document.createElement(“td”);

   td1.innerHTML = ‘<select name=”reminder”><option value=”email” selected>Email</option><option value=”sms”>SMS</option><option value=”alert”>Pop-Up</option></select>’;
   td2.innerHTML = ‘<input name=”min_value” type=”text” />’;
   td3.innerHTML = ‘<img src=”images/1244107465_time_delete.png” onclick=”removeRow();” />’;
   tr.appendChild(td1);
   tr.appendChild(td2);
   tr.appendChild(td3);   
 
   document.getElementById(“mainTbl”).getElementsByTagName(“tbody”)[0].appendChild(tr);
  }

Here createElement() creates the clone of <tr> and <td>. All the table elements HTML contents are get added using the appendChild() function as seen above.

By Runtime clone creation, we are creating delete event also for deleting the created clones using the removeRow(). This function will fire on click of  delete icon.

Idle Timer

The idle timer is built on jQuery and provides two custom events: idle.idleTimer and active.idleTimer, which fire when the user’s idle state has changed.

This example has an idle timeout of 7 seconds. When you move your mouse over the page or start typing, you’re considered “active”. The status above the textarea changes to indicate your current state.

 

 

 

 

(function($){
  
        $(document).bind(“idle.idleTimer”, function(){
            $(“#status”).html(“User is idle :( “).css(“backgroundColor”, “silver”);
        });
       
        $(document).bind(“active.idleTimer”, function(){
             $(“#status”).html(“User is active :D “).css(“backgroundColor”, “yellow”);
        });
       
        $.idleTimer(7000);
   
   
    })(jQuery);

<div id=”status” style=”padding: 5px;”>&nbsp;</div>

Note:

Running this Idile Timer successfully need to include the jquery.js and idletime.js. You can download the idile timer from this below URL,

http://paulirish.com/demo/js/idle-timer.js

Open All External Links in a New Window

Using Jquery, you can customize the external links of your site will open in a new window by the following format.

$(document).ready(function(){
         $(“a[@href^='http']“).attr(‘target’,'_blank’);
});

 Not process links to the same domain, and pdfs can done as follows,

 

 


var host = window.location.host.toLowerCase();
$(“a[@href^='http']:not([@href^='http://" + host + "']):not([@href^='http://www." + host + "']), a[@href$='.pdf']“).css({outline: ‘solid 1px red’}).attr(“target”, “_blank”);

Follow

Get every new post delivered to your Inbox.