»
S
I
D
E
B
A
R
«
JavaScript: The simple difference between jQuery’s bind() and live()
Mar 5th, 2010 by admin

This morning I am delving into some JavaScript code, and decided once and for all to find out what exactly the difference was between the two functions: bind() and live(). It turns out, there is a very minor but important difference.

According to the jQuery API documentation, bind( action, function ) is used to give an event to all existing matches. On the other hand, live( action, function ) not only gives the event to all existing matches, but all future matches.

So, concluding my thoughts, if you are doing work where you are adding a lot of things to the page — i.e. using AJAX — that need to have an existing action handler applied to them, then you should stick with live(). Otherwise, learn to love bind().

Nick

JavaScript: Bug using .focus() for legacy coding and FF
Mar 4th, 2010 by admin

Hopefully you have found this article before you bashed your head open with your keyboard; if not, please seek medical help before continuing.

When using the .focus() method today, I came across an issue with Firefox — though, I would assume it is for all legitimate browsers. If you are having trouble getting .focus() working, kindly apply the following code.

setTimeout( function(){document.getElementById('field').focus();}, 10 );

Unfortunately, I have no reason why it works, I merely know that it does work.

Nick

Passing/calling a function by its name in JavaScript
Sep 14th, 2009 by admin

Today, I needed some code that would allow me to pass a function name as a parameter, and then call that function on the fly. Well, today, I found out something very useful. You can actually pass the function itself into the function, and then use the built in method called apply to call the function.

To help you more easily understand this, I have included an example:

function callMe( useThisFunction, parameter )
{
    useThisFunction.apply( useThisFunction, Array(paramater) );
}

/**************************************************/

function test( outputMe )
{
    alert(outputMe);
}

/**************************************************/

document.onload = function()
{
    callMe( test, ‘Look at me, I have been outed!’ );
};

Enjoy!

Nick

Using JQuery to clear a div [or an element]
Aug 7th, 2009 by admin

JQuery continues to make things easy on us. The following code will remove all of the children elements from the parent.

$('#idOfElement').empty();

Now, that was easy, eh?

Nick

»  Substance: WordPress   »  Style: Ahren Ahimsa