Free up your time and revolutionize your design with simple web development.

jQuery for Beginners: Selectors, Hierarchies and Attributes, Oh My!

Posted: September 13th, 2009 | Author: Kurt | Filed under: Programming | 14 Comments »

jquery

This is the first part of a series of jQuery tutorials for designers and web developers. You can automatically receive future additions to the tutorial series by subscribing to the RSS feed, or by following me on Twitter.


jQuery is a javascript framework that makes it easy to edit markup and styles on a page, build advanced user interfaces, and update data sources with AJAX (that is, Asynchronous Javascript and XML). In the words of the jQuery developer site:

jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.

Today, we’ll go over the first steps and the one major benefit of using jQuery: selecting and manipulating markup and CSS. First, you’ll need to include jQuery in your page:

 <html>
      <head>
           <script type="text/javascript" src="jquery.js"></script>
           <script type="text/javascript">
                // this is where our jQuery commands will soon go
           </script>
...

For the course of this tutorial, I’m going to refer to a sample unordered list with two text spans. It looks like this:

<ul id="list">
     <li class="listitem" id="listitem-1"><span class="smallcaps">Hello</span> and thank you for reading.</li>
     <li class="listitem" id="listitem-2">I hope you like this tutorial.</li>
</ul>

Notice that I adorned the markup with the id and class attributes. That’s so I can select portions of the markup with jQuery. Remember, the id attribute should be unique, but the class attribute will be repeated on multiple elements, usually in line with an attached stylesheet. For this tutorial, though, assume that there is no CSS modifying the basic behavior with the markup.

Now, you can use jQuery to apply a style to the entire list. First, we’ll create a function that is loaded when the page is opened, we’ll select the entire list with jQuery, and apply a CSS style to make the text blue via the color attribute.

$(document).ready(function() {
     // anything in here will be loaded when the page is ready
     $("#list").css("color", "blue");
});

Here’s the rundown: the $() command is used to select markup or series of markup from the page. You can select elements by id, class, or element name. To select by id, add a # to the front of the id name inside the parenthesis. For instance, to select the first li in the example above, use $("#listitem-1"). Classes are selected by putting a . before the class name, and element names are selected without adding any modification.

You can use hierarchies to drill down through the markup and select specific portions of the page. The syntax is similar to XPath, where you place a > in between the names discussed above. Here’s a quick snippet partially based on the sample above:

$("#list > li") // all list items in the list with an id of list
$("div > .nav > a") // all links underneath the div elements that belong to an element with a class of nav

Once you select an element, you can apply a CSS style to them with the css(attribute, value) command. Attributes are things like color, font-weight, and display, and values are things like #cccccc, 1.2em, and inline. So, you can make the span in the section above render in small-caps with the following:

$(".smallcaps").css("font-variant", "small-caps");


Next time, we’ll walk through some of the more advanced selectors. Also, expect tutorials on AJAX and jQuery UI.


14 Comments on “jQuery for Beginners: Selectors, Hierarchies and Attributes, Oh My!”

  1. 1 devmarks.com said at 2:28 pm on September 13th, 2009:

    jQuery for Beginners: Selectors, Hierarchies and Attributes, Oh My! …

    Today, we’ll go over the first steps and the one major benefit of using jQuery: selecting and manipulating markup and CSS….

  2. 2 Pedro Cortizona said at 11:23 am on September 14th, 2009:

    I’m a Web Designer not a Programmer, so my knowledge of JavaScript is minimal.

    This very small tutorial so far has taught me SO much. Explaining the syntax in a very simple, 3rd-grader languange is something I haven’t found… although the W3Schools.com is somewhat simple too, but it doesn’t compare to the simplicity of this post.

    Looking forward to the next one.

    Thanks a million.

  3. 3 LuK said at 6:20 pm on September 14th, 2009:

    How about multiple css values, how do I separate them? Is it done like in the following example or do I have to separate them with semicolons or something else? How about shorthand css like background, do I need to put in backgroundColor instead? And could I also change a whole css shorthand value like in the second example?

    $(”p.article”).css(”background”, “#cccccc”, “text-align”, “justify”, “color”, “#ffffff”);

    $(”p.article”).css(”background”, “#cccccc url(./bg/somebg.png) no-repeat”);

  4. 4 Kurt said at 9:34 am on September 15th, 2009:

    The second example is correct, but as for multiple CSS attributes added in a single command, you should do it like this:

    $(”p.article”).css({’background’ : ‘#CCCCCC’, ‘text-align’ : ‘justify’, ‘color’ : ‘#FFFFFF’});

    Same deal as before, but you surround the thing in brackets (so jQuery knows it’s a whole array of values and doesn’t give up before you’re finished outlining CSS), and you put a “:” in between the attribute and the value.

    Hope this helps!

  5. 5 LuK said at 10:52 am on September 15th, 2009:

    Great, learned something again =)! thx

  6. 6 Destillat KW38-2009 | duetsch.info - GNU/Linux, Open Source, Softwareentwicklung, Selbstmanagement, Vim ... said at 1:18 am on September 18th, 2009:

    [...] jQuery for Beginners: Selectors, Hierarchies and Attributes, Oh My! [...]

  7. 7 Rad said at 2:29 am on September 18th, 2009:

    It doesn’t get any simpler than this i’ve been scouring all over the ‘net looking for the best way to learn jquery or at the very least understand it’s inner workings, I’m grateful I stumbled upon this post so simple and yet so informative. :) excellent looking forward to the next post.

  8. 8 Collection of top jQuery tutorials, tips-tricks & techniques to improve performance « Technosiastic! said at 4:52 am on September 24th, 2009:

    [...] jQuery for Beginners: Selectors, Hierarchies and Attributes, Oh My! [...]

  9. 9 links for 2009-10-01 said at 7:08 pm on October 1st, 2009:

    [...] jQuery for Beginners: Selectors, Hierarchies and Attributes, Oh My! – Cherrysave (tags: jquery tutorial) [...]

  10. 10 links for 2009-10-07 « Cache 242’s Blog said at 10:11 am on October 7th, 2009:

    [...] jQuery for Beginners: Selectors, Hierarchies and Attributes, Oh My! – Cherrysave (tags: jquery tutorial javascript programming webdesign tutorials) [...]

  11. 11 Gangster64 said at 8:45 am on October 23rd, 2009:

    Bank spends or invests 30 back into the economy. ,

  12. 12 JQuery for Beginners: Using Simple JQuery Effects for a Rockstar User Interface - Cherrysave said at 11:30 am on November 6th, 2009:

    [...] Interface This is the second part of my series on JQuery for Beginners. Be sure to check out the first part of the JQuery tutorial series where I introduce the framework and show readers show to use selectors, hierarchies, and [...]

  13. 13 Curso de jQuery (4): Selectores, Tercera Parte | emenia.es said at 10:36 am on November 13th, 2009:

    [...] The Complete Series Woorkup: jQuery Lesson Series: Introduction to Selectors Cherrysave.com: jQuery for Beginners: Selectors, Hierarchies and Attributes, Oh My! Comparte este artículo en tu red [...]

  14. 14 Robin said at 11:53 am on March 3rd, 2010:

    Answer to LuK’s question about adding multiple css atributes:

    You can also add a css class on something, like in your case something with the class of article inside a p tag. Do like this.

    $(”p.article”).addClass(”yourCssClass”);


Leave a Reply