Passionate Development From Journeyman to Master

jQuery basics

What's the most mundane and repeated code that you have to write in Javascript?

Most likely it is getting an element by id or getting some elements by name.

Getting element by id
Old school:
<pre>var theElem = document.getElementById(‘theElem’);</pre> jQuery:

var theElem = $('#theElem');

That's basic enough, it's pretty similar to Prototype. But worth noting, In Prototype, you don't need to use the # sign. I've fallen into this couple of times when making the transition to jQuery.

Getting elements by name

Today I learned something new. I’ve found the jQuery’s equivalent to Javascript’s getElementsByName
Old school:

//assuming there's only one
//or we're interested in the first element found
var theElem = document.getElementsByName("theElem")[0];


var theElem = $('[name=theElem]');
//If iterating is neded
$('[name=theElem]').each( function() {
	//do stuffs

$('[name=whatever]') simply means find all elements that have attribute name equals to whatever.