SpyreStudios

Web Design and Development Magazine

  • Design
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

Selectors, Animation, and AJAX – jQuery Tutorial And Examples (Part Two)

June 16, 2009 by Laptop Logic 19 Comments

jQueryBy now I’m sure you have grasped at least the basics and simplicity of the jQuery library. Now it’s time to move on to some more in depth and practical techniques. jQuery isn’t just for hiding and showing content as we saw in the first part of our series, it can also perform many powerful tasks for us with just a few lines of code.

For part two of our jQuery series, we will have a look at some special selectors, the animation method, and simple AJAX calls. Be sure to check out the demo for each section for an idea of how you could implement some of these ideas and scripts!

Special Selectors

We have learned that selecting elements with jQuery is very similar to selecting elements with CSS. For example, say we wanted to select all paragraphs within a certain div, we could do something like this:

[html]
$(function(){
$(‘#my_div p’).click(function(){
//your code here
});
});
[/html]

This is all fine and dandy for simple selections and manipulation. But what if we wanted to get more specific or use a different selection technique? Luckily, jQuery has some great built in functions for selecting special elements.

The :first method

Let’s say that we want select the first and only the first paragraph of a certain div element and manipulate the styles. We could give that paragraph a special class, but what if we couldn’t/didn’t want to do that? This is where the helpful ‘:first()’ method comes in. We can accomplish our goal like so:

[html]
$(‘#dummy_section p:first’).css(‘color’, ‘blue’);
[/html]

Simple right? Moving on…

The :not method

jQuery 1.3 was released with a special new selector method, called the ‘:not’ method. The :not method does what it sounds like it does. It is used to select certain elements that do not contain other elements. In other words, say we have an unordered list with a few list-items tags. We want to select all of the list-items that do not have a class of special_class. This is accomplished as seen below:

[html]
$(‘#dummy_section ul li:not(.special_class)’).css(‘color’, ‘red’);
[/html]

Above, we target the unordered list. Select the list-items that do not contain the special_class and alter the CSS color property. The :not method can be extremely helpful when working with external data and third party API’s.

The :eq() method

The :eq method is a zero based index method to match the number given. Sound tricky? It’s not. If you have ever programmed before you will immediately understand this. Basically, inside of the :eq() method, we use a number for the element we want to match. If we want to match the third paragraph, we would use:

[html]
p:eq(2)
[/html]

Why two? Because the counting starts at zero, so the first element would be 0, the second would be 1 etc. Let’s look at an example:

[html]
$(‘#dummy_section p:eq(2)’).css(‘color’, ‘green’);
[/html]

Above, we select the third paragraph within the div named ‘dummy_section’ and alter the CSS, that’s all there is to it!

Be sure to check out the selectors demo to see all of the examples in use!

Selector Resources

  • jQuery Selectors
  • The :first filter
  • The :not filter
  • The :eq() filter

Easy Animation

Not only does jQuery come packed with special effects such as ‘hide()’ and ‘show()’, but it also comes with the custom animate() method. The animate method allows us to define almost any type of custom animation, including how long the animation should last.

Link Nudge

A popular example of using the animate method is the ‘link nudge effect’ where the links are slightly animated when moused over. Say we have an unordered list and we want to link nudge the links within this list. Example below:

[html]
$(‘ul li a’).hover(function() { //mouse in
$(this).stop().animate({ paddingLeft: ’10px’ }, 200);
}, function() { //mouse out
$(this).stop().animate({ paddingLeft: 0 }, 200);
});
[/html]

Above, when the links are hovered, we change the left padding to 10px over a speed of 200ms. When the user mouses out, the padding is changed back using the same speed.

Image Effect on Hover

Lets have a look at another practical usage of animate. Say we have an image, and we would like the image to have a nice effect when hovered. We want the image to have 80% opacity by default, and when the user mouses over the image we want to change the opacity to 100% over a certain amount of time. This can be used to for a nice effect on image galleries or ad banners. he code below accomplishes our task.

[html]
$(‘.hover_image img’).css(‘opacity’, 0.8).hover(function(){
$(this).stop().animate({opacity: 1}, 500);
}, function(){
$(this).stop().animate({opacity: 0.8}, 500);
});
[/html]

Above, we change the default opacity to 80%. We then set an event for when the user mouses over and out of the image, changing the CSS accordingly.

Be sure to check out the animate demo we have setup to see all of the examples in action!

Simple AJAX

For the last part of our article, lets see how simple AJAX calls can be with jQuery. For our example, we will have an external HTML file with some content that we want to display. We only want to load this file and display it’s content when the user clicks on a certain link, thereby decreasing our initial page load. It is as simple as using the .load method, which automatically uses a GET AJAX request by default. Lets look at how we would do this.

[html]
$(‘p.load_it a’).click(function(){
$(‘#demo_content’).load(‘external_file.html’);
return false;
});
[/html]

When our link within the paragraph class of ‘load_it’ is clicked, the file named external_file.html will be loaded into the div with an id of demo_content. That’s all that is required for basic AJAX loading!

Be sure to check out the AJAX demo to see this example in action!

AJAX Resources

  • jQuery AJAX Docs
  • jQuery load method

That’ll do it for now, hopefully you have improved your jQuery skills or learned something new. Let us know if you have any comments or questions in the comment section below. Happy coding!

Filed Under: jQuery, Tutorial

Comments

  1. Jason says

    June 16, 2009 at 9:21 am

    Good, basic jQuery stuff everyone should learn. Thanks!

  2. Jon Phillips says

    June 16, 2009 at 1:35 pm

    @Jason: Glad you liked the post! :)

  3. Mohamed Aslam says

    June 16, 2009 at 3:02 pm

    Nice Tutorial! Keep it up!

  4. Kyle says

    June 16, 2009 at 6:34 pm

    very well explained. Made perfect sense with only one read through. Keep up the good work!

  5. jlbraaten says

    June 16, 2009 at 9:56 pm

    Really great article. I have to put Javascript on my list of things to do.

  6. lexaz01 says

    June 16, 2009 at 11:16 pm

    I recently came accross your site and have been reading along. I thought I would leave my first comment. I dont know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this site very often.

  7. Rubens says

    June 17, 2009 at 9:29 am

    Great article, thanks!

  8. WebDevVote.com says

    June 18, 2009 at 6:48 am

    You are voted!
    Track back from WebDevVote.com

  9. Keith Johnson says

    June 18, 2009 at 8:57 am

    Very informative article about jQuery, bookmarked! Thanks again.

  10. Phaoloo says

    June 20, 2009 at 12:27 am

    Nice guideline about jQuery.

  11. jason says

    June 20, 2009 at 10:09 pm

    Read, learned and implemented. nice one, thx!

  12. Neelam says

    October 23, 2009 at 1:34 am

    Hey…. Im new to JQuery…. Great Help…..Good One

  13. builder says

    April 25, 2010 at 1:23 pm

    Great tutorial. Thanks!

  14. wynajem says

    June 27, 2010 at 7:21 am

    Awesome article ;)

Leave a Reply

Your email address will not be published. Required fields are marked *

Please prove you're human *

Recent Posts

  • What Factors Determine the Best Digital Marketing Agency?
  • 31 Fresh Design Elements for Spring and Easter
  • 10 Templates for Music Concert Flyers
  • How to Build a Web Scraper Using Node.js
  • Best PHP Books, Courses and Tutorials in 2022

Archives

  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • May 2008
  • April 2008

Categories

  • Accessibility
  • Android
  • Apps
  • Art
  • Article
  • Blogging
  • Books
  • Bootstrap
  • Business
  • CSS
  • Design
  • Development
  • Ecommerce
  • Fireworks
  • Flash
  • Freebies
  • Freelance
  • General
  • Giveaway
  • Graphic Design
  • HTML5
  • Icons
  • Illustrator
  • InDesign
  • Infographics
  • Inspirational
  • Interview
  • Jobs
  • jQuery
  • Learning
  • Logos
  • Matrix
  • Minimalism
  • Mobile
  • Motion Graphics
  • Music
  • News
  • Photoshop
  • PHP
  • Promoted
  • Rails
  • Resources
  • Showcase
  • Tools
  • Tutorial
  • Twitter
  • Typography
  • Uncategorized
  • Usability
  • UX
  • Wallpapers
  • Wireframing
  • WordPress
  • Work

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

SpyreStudios © 2022