To reference a jQuery function, you must supply a namespace. This can be written as ‘jQuery’, but is conventionally written as ‘$’. For example;
// is the same as
Whole elements can be selected, including those with particular classes or IDs. The class and ID selectors are ‘.’ and ‘#’ respectively, the same as in CSS.
// select all paragraphs
// select all paragraphs with the ‘main’ class
// set the value of the element with the ID of ‘unique'
Adding and removing classes to tags can be done easily with the addClass() and removeClass() functions.
You can test if an element has a class by using the hasClass() function.
if ( $(“p”).hasClass(“important”) )
Functions are also chainable to help with more complicated selection and tasks.
// add an “impressive” class to all paragraphs within the “parent” div
As you can see from these code examples, jQuery allows you to do a lot with very little code. It’s this simplification and separation from the main HTML structure that makes it so useful for web developers. Use of jQuery has risen dramatically since its launch in 2006, with big names like the BBC, MSNBC and Digg now using it prominently. This popularity should see it stick around for a while, and is a useful addition to any developer’s toolkit.
Comments are closed.