Saturday, October 20, 2012

two random things of JavaScript and jQuery

  1. Get or set the HTML inside of a div in JavaScript:
    var whatitwas = document.getElementById('whatever').innerHTML;
    document.getElementById('whatever').innerHTML = whatitisnow;

     
  2. bind/live/delegate in jQuery:
    $('#foo').bind('click', function() {
       alert($(this).text());
    });
    $('#bar').live('click', function() {
       alert($(this).text());
    });
    $("table").delegate("td", "hover", function(){
       $(this).toggleClass("hover");
    });
    1. bind effects immediate entities
    2. live effects those that may appear later too
    3. I've never used delegate, but, per Justin Pope:
      1. delegate takes three parameters
      2. delegate is a marginally more performant version of live
      3. delegate must be declared before the thing it effects comes into memory

 
 

Note: After I wrote the above today, Steve Flitcroft told me over Twitter that I should use .on instead of .live or .delegate.

 
 

Another Note: (writing this a day after I wrote this blog posting) Mr. Flitcroft also gave me a link to: http://www.ultimatewebtips.com/why-jquery-live-is-a-bad-option-to-use/

No comments:

Post a Comment