If you've got a good understand of CSS selectors, then you're already familiar with how to query the DOM.
Background
CSS1
jQuery uses CSS1, 2 and parts of CSS3 at the heart of all it's queries, just in the same way you would use CSS to target an element or group of elements to style.
In CSS, to hide an element we could use:
.hidden { display: none; }
Using jQuery we can achieve the same effect by finding all the elements whose class is hidden and hiding them like this:
$('.hidden').hide();
Equally to target an ID, in CSS we use:
#header { display: none; }
Which is this in jQuery:
$('#header').hide();
CSS2
More complicated CSS selectors can be used using CSS2. Here we are styling all links that have an attribute of type="application/pdf" with a nice little icon, and doing something cool when they click on the link.
a[type="application/pdf"] {
background-image: url(/images/pdf.gif);
padding-left: 20px;
}
$('a[type="application/pdf"]').click(doSomethingCool);
CSS3
Taking the above example further with CSS3 is easy. Let's assume the markup already exists, and we want to find all the anchors that link to pdfs:
a[href$=".pdf"] {
background-image: url(/images/pdf.gif);
padding-left: 20px;
}
$('a[href$=".pdf"]').click(doSomethingCool);
You should check the full jQuery selector documentation for which CSS3 selectors are supported.
$('.hidden') can be slow if you have a 1,000 elements on the page.
Example
So you've got your built web page, fully styled and there's no interaction layered on yet. Using these CSS selectors we can target elements and hook actions.
In this simple example we have a registration form. We have terms and conditions that if the user wants to read, they'll slide out to be shown.
The terms are both linked to and hidden on the page. If the user clicks on the link and JavaScript is enabled, we'll handle the click action ourselves by sliding the text in to view.
$(document).ready(function () { // when the DOM is ready do:
$('A.reveal').click(function () {
$('#terms').slideDown();
return false; // don't follow the default link action
});
});
This code breaks down as follows:
$(document).ready(function () {
Run the JavaScript in this (anonymous) function when the document has loaded.
$('A.reveal').click(function () {
Find all the anchors with the class 'reveal' and hook a click event.
$('#terms').slideDown();
When the user clicks, find the element whose id is 'terms' and fire the slideDown animation. You should note that these effects are smart enough to know whether the target element is already shown or not. So in this particular example, if the terms element is already being displayed, it won't try to slide it down on clicking again.
return false;
Finally, this stops the browser following the link's href (which is the default action).
Taking it further
The best way to understand the selectors and how they work is by injecting jQuery (using the bookmarklet) in to a page and testing out selecting using firebug.
December 7, 2007 at 20:45
Of course you can also do:
$('.hidden').css('display', 'none');I've always wondered if there is any speed difference between that and $('.hidden').hide();
February 5, 2008 at 03:55
Perhaps I'm doing something wrong, but I can't seem to get binding a click handler to elements of a certain class to work. I'm using jquery 1.2.2 and this code
$("classname").click(function(){ //some things });
html is
Any ideas?
TIA, Tom
February 5, 2008 at 03:57
I actually put the wrong code in my previous comment, should be
$("img.classname").click(function() { //stuff });
My html is a simple img with the class classname. I've tried this on anchor tags as well to no avail.
February 5, 2008 at 09:40
@Tom - the second code looks right, so my bet is that it's not wrapped in a 'ready' statement:
April 27, 2008 at 16:05
Hi,
thanks for a great site :) learning a lot on here ;)
I've got a problem though with controlling this sliding down effect in a very specific manner... - as I'm new to this I would really appreciate any help, or pointers...
I'm collecting my samples on this page here: http://dev.eyedea.eu/jquery/pages/slidepanel.html
Basically, I would like to control the sliding element which reveals my content. I managed to get it working - it slides down fine. But I would like to stop it once it reaches a certain position, ie the very bottom of the window, and then assign it the CSS property of fixed (allowing page content to scroll behind it).
My sample #6 works with a final result of how the page should look - m problem is that the panel slides out of the window entirely, then jumps into position.
I think I am close - but cannot figure out to stop the sliding motion at the right moment (and if statement of some sorts I imagine). So the whole thing looks very clumsy ... my JS code (in addition to the JQuery link):
Sorry to go on so long - but I have been trying to find a solution desperately and was trying to explain as clearly as possible. Any thoughts would be greatly appreciated....