jQuery 3 - Understanding jQuery |
Written by Ian Elliot | |||||||
Thursday, 16 June 2016 | |||||||
Page 3 of 3
Let's break it down. First we retrieve 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 through 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. In fact there is an even more compact way to write this, but for this we need some additional jQuery methods. We will return to this topic in a later chapter, but let's take a quick look at the sort of thing that is already possible.
jQuery Wins With jQuery you usually find that there is a pure jQuery way to do anything you struggled to do directly with the DOM. In particular, most of jQuery's methods are more sophisticated than you might expect. We have already met the text() method that will retrieve or modify the text of every element in the result. This is fine as long as you want to set the text of every element to the same string. If you want to set each one to a different string then it seems that we have to resort to the trick of using a loop to scan through each DOM element in the result. However the text() method and many other jQuery methods have an extra way you can use them. The text() method accepts a function with the form:
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. That is when you use .text() jQuery loops through each element in the result and calls the function passing it the number of the element in the result and its current text. The elements text is then set to what ever the function returns. This means that we could write the above example without a loop and using nothing but jQuery:
In this case the text property is set to its index in the result set. 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.
Available as a Book:
buy from Amazon
Also Available:buy from Amazon
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 ) |