Escolar Documentos
Profissional Documentos
Cultura Documentos
Selectors
Learning Objectives
By the end of this lecture, you should be able to:
Comfortably use jQuery to select based on tag name, ID name, class
name
Select using descendant selectors
Comfortably look up and apply selections using various attribute
selectors
Comfortably look up and apply selections using filters
Understand how to apply multiple functions to a single selection on a
single line using chaining.
ID: Suppose you had a div tag withan id of contact_info. You could select this ID in
the typical JavaScript way using:
document.getElementById('#contact_info');
$('#contact_info');
$('h2');
Class: While it selecting based on class is certainly doable using standard JavaScript,
this is one of the many cases in which results can vary depending on the browser being
used. So we will forgo discussion on how to do it using JS for now.
The jQuery way:
$('.name_of_the_class');
EXAMPLE: To hide all elements to which the class navItem has been applied:
$('.navItem').hide();
$('#favoriteFruit img')
The key here is the order: First you select the main item, then place a space, and then the inside item. In
the example above, we first select the favoriteFruit id. We then place a space followed by img.
The result is that all images anywhere inside a tag with an ID of favoriteFruit will be selected.
You can then do something such as:
$('#favoriteFruit img').width('50px');
$('#favoriteFruit img').height('50px');
//will set all images inside the favoriteFruit ID section
//to 50 pixels high and wide
$('#navigationBar a').hide();
//will hide all hyperlinks inside the
//section that has an ID called navigationBar
EXAMPLE: descendent_selectors.htm
However, there are many other scripting commands besides function calls that you can
provide as the value to onclick. For example:
<input type= "button" value= "Greet Me" onclick= "alert('Hello!')">
$('img[src^="ball"]');
//selects images that have an src that begins with the word ball
//e.g. ballOfWax.jpg, ballAndChain.jpg, balloon.jpg, etc
$('img[src*="ball"]');
//selects images that have an src that contains the word ball
//e.g. redball.jpg, greenball.jpg, balloon.jpg (!), etc
$('a[href$=".ppt"]');
//selects all hyperlinks that end with the text .ppt
//e.g. lecture1.ppt, intro_to_jquery.ppt, etc
$('p:not(.emphasize)');
//selects every paragraph on a page that does NOT apply the .emphasize class
$('li:has(a)'); //selects every list item that contains a hyperlink
$('li:contains("hockey")');
//selects every list item that contains the word hockey
$('a:not([href$=".ppt"])');
//selects every anchor tag that does NOT link to a powerpoint file
$('li:hidden'); //selects every list item that is hidden
//For example, if you have hidden some items previously, you can
//make them visible again with: $('li:hidden').show();
Examples
The following will hide all of the images on a page
$('img').hide();
Suppose you wanted to limit this list so that you only hide images that include an alt
attribute:
$('img[alt]').hide();
Suppose you wanted to further limit this list so that you only hide images that include an
alt attribute that has the word ball anywhere inside of it:
$('img[alt*="ball"]').hide();
NOTE: This steps used in these examples also serve as a great way to show you how to
get jQuery to work the way you want it to. That is, start with your selection as simple and
broad as you can. Then work on refining your selection further and further, testing your
page each time, until you get it to work the way you want it to.
Chaining functions
If you want to do several operations on a selection, you could write out each function one
at a time OR, you could simply concatenate the functions, one after the other.
Example: Suppose you want to take all of the images on a page and set them to the
identical size (lets say width=200 pixels, and height = 200 pixels), and then fade them in
over 1 second. You could do so with:
$('img').width(200);
$('img').height(200);
$('img').fadeIn(1000);
Instead, however, you could save time and space by chaining the functions together like
so:
$('img').width(200).height(200).fadeIn(1000);
This is not simply cosmetic. Chaining functions turns out to be faster and more efficient.
We will discuss more about efficient coding as we progress through the course. For now,
however, if you have a group of related functions that you are planning on applying to a
particular selection, you should try and chain them.
File: selectors_contd.htm