Getting Started With jQuery - The DOM |
Written by Ian Elliot | ||||||
Monday, 08 October 2012 | ||||||
Page 2 of 2
jQuery IdiomsConsider for a moment:
This gets the DOM object corresponding to the div element with id div1 and stores this as a jQuery object in divs. It then uses the jQuery method text to retrieve the text it contains and displays the text using an alert. Notice the text method will collect all of the text in each of the DOM objects selected but in this case there is only one. There is nothing wrong with this code snipped but it doesn't look like most jQuery programs. It is more usual to write jQuery using the fluent style of calling methods. So you would write the above as:
In general programmers tend to write as many jQuery methods calls as possible, one after another until they reach the final result they want. jQuery also defines, for reasons that don't really matter much, the dollar symbol as the global jQuery object. That is
and most programmers would write the example as:
This makes the code more compact, but it also makes it look more intimidating to the beginner. But if you remember that whenever you see "$"you simply read "jQuery" it becomes understandable. There are a few other jQuery idioms that will become apparent as we progress.
DOM versus jQueryIn most cases once you have a jQuery object it is best to try to make use of it to get the job done. However, it is always worth remembering that you can make use of the DOM objects directly. Usually this isn't a good idea because jQuery irons out any variations in browser and implementations. If you can do the job in jQuery then it is usually the best approach. For example, suppose you want to set the text in the div with id div1. The jQuery way is:
The text method can be used to set text on almost all elements apart from some form element. It also returns a jQuery object and so allows you to carry on chaining jQuery method calls.It also sets the text on each fo the selected DOM elements in the array. However if you want to work with the DOM object directly you can. For example, DOM objects have an innerHTML property which corresponds to what it between the tags - in the case of a div what is between the <div> and </div>. So to use the DOM object rather than jQuery you would write:
This works but doesn't process the text to remove HTML special characters. You could have written this as:
and in this form it looks a lot like the jQuery text method vesion - but it isn't. The whole point is that $('#div1')[0] isn't a jQuery object but a DOM object. Beginners often make the mistake of writing things like:
which doesn't work because DOM objects don't have jQuery methods. Always remember that any DOM objects you reference in the array are just that - DOM objects and not jQuery objects. There is a way to put the previous mistake to rights. You can wrap any DOM object in a jQuery object by writing
So to make the above incorrect example work you would use:
Now you begin to see how cryptic jQuery expressions become. Let's break it down. First we retrive the DOM object:
Next we wrap the DOM object in a jQuery object:
Notice that at this point we are back were we started with a DOMobject that is the first element of an array - I never claimed that this was a sensible thing to do! Finally we use the text method which sets the text of all of the DOM objects in the array:
Notice that this conversion to DOM object and back to jQuery object can have some practical use. For example if you use jQuery to get all of the divs in the bigdiv class:
you can now step through each DOM object in turn, wrapping it in a jQuery object and setting its text to something:
Which of course can be written in a more compact form with the jQuery expression on a single line:
So if you want to step though the DOM objects and do something complicated to them then this is possible and you can re-wrap them in a jQuery object to make it easier. jQuery WinsWith jQuery you usually find that there is a pure jQuery way to do anything you struggled to do directly with the DOM. In this case the text method accepts a function with the form function(index,oldtext), which is called for each DOM object in the array. The index is the index of the DOM object in the array and the oldtext is its current text content. The string that the function returns is set as the text for the DOM object. This means that we could write the above without a loop and using nothing but jQuery:
Never underestimate jQuery! Summary
Coming NextSo what else is there to learn about jQuery? You need to learn more sophisticated ways of selecting elements, what methods are available to make changes to elements and so on. Then of course there are the other areas that jQuery helps with - events, Ajax and the UI. More in future articles.
Chapter List
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
Comments
or email your comment to: comments@i-programmer.info
|
||||||
Last Updated ( Sunday, 19 February 2017 ) |