Learning jQuery
The time has come to learn jQuery!
Here, you can download jQuery. Scroll down to the “Current Release” bit, right click on the download link and select “save as”. Save it to a folder called “jquery”. Make sure you get the path to the javascript file right in your html.
Some Initial Learnings About jQuery
You can reference the jQuery selector using jQuery(‘a’) or $(‘a’). Obviously, the $ format is shorter, so I’ll be using that where I can.
The selector is great. jQuery(‘a’), for example, selects all links on the page en masse. You can then do “something” with them. jQuery(‘a’).hide() hides all links in one go. Implicit iteration, they call it. Genius, I call it. You don’t need to use a for loop to iterate through every element in tha array is what I mean, bro.
You can chain functions so jQuery(‘div’).hide().text(‘Fine thanks, how are you’).show() means:
- get all the divs on the page (not just the first one)
- hide them
- change their content to be ‘Fine thanks, how are you’, and then…
- display them
There is a jQuery function, ready(), which indicates when the DOM has been loaded, but before the page content has been loaded. You might be tempted to use this on functionality you want to invoke only when the DOM is ready. But there is an argument for placing that javascript functionality immediately before the </body> tag, because by that late point all elements on the page have been loaded anyway. As you don’t then have to code the $(document).ready(), less code means faster performance.
Just off to do a bit more reading!