March 30, 2012

JQuery Chaning

One of the really interesting things about JQuery is the chaining mechanism. It enables you to write actions after one another in a chain of actions, a bit unlike many other languages handles thins. JQuery provides chaining feature to call multiple method calls in single statement.

For instance:
$('#Table1').fadeIn().fadeOut();

will first make  #Table1 fade in and then fade out, creating an animation.  The chain makes consecutive actions show up in relation also to when they will be executed and that gives a more readable code.

This is based on the design pattern where method calls return a JQuery object so that once the method is executed it will also return a JQuery-wrapped object that has all the other methods of JQuery available. In that case, we must therefore consider that JQuery selectors are not giving us the table object but a JQuery-wrapped table object as is the case in this example. This can hide methods available in the DO on for instance the table objects since the selector doesn't provide us with the table object but the JQuery object.

To get the Raw DOM element inside the JQuery element you call: 

$("table").get(0); 
or
$("table")[0]; 
 
This will give you access to that object and therefore also access to methods containd in the DOM objekt.



No comments:

Post a Comment