First I want to make sure we are all on the same page about Jquery. I am going to assume that since you are reading this you know that Jquery is a javascript library, but you don't have the faintest clue on how to write code using Jquery. Sure you have jquery installed on your website and have used a couple of plugins, but as far as you know you have to assign this class to an element then the plugin works. Well I am here to change that to make your life easier, like the jquery slogan "Write Less, Do More".
This guest article was written by Hayes, the 13 year old web developer and designer.
Have some web development, web design or blogging skills? Contribute to CatsWhoCode!

Download jquery here.

First lets go over the basics of jquery coding. All jquery starts with a “$” no matter what! In all of your jquery documents ( which is just a javascript file) you need the following code in your document, this code is to load your functions if you do not add this code your jquery will be useless.

$(document).ready(function() {
// all of your code goes here
});

What that code does is simply load all of your functions quickly, without slowing down your page it is only in jquery. For everyone that knows a little about javascript you probably know the “window.onLoad()” function, it does the same thing as “$(document).ready(function() {” but it waits until the entire page finishes loading then it loads in the javascript. This makes your javascript very slow especially if you have large images, the jquery code loads all of the javascript once all of the important stuff has loaded. Like the HTML and CSS structure, which means your javascript are now the coolest kids on the block.

Now that we have that out of the way let us go over calling classes and whatnot, look here:

$('.comments')

All this does is find all of your content inside the CSS class “.comments” , if your replaced the ‘.comments’ with something like ‘p’ jquery would find all of your paragraphs. Same thing for CSS id’s just use a “#” instead of a dot.
So now lets build on our code, let’s add a CSS class to everything inside inside “.comments”. Call it stripe, and it would add a background of a light grey or something. Also note that I am referring to comments in wordpress.

$('.comments').addClass('stripe');

Now all of our comments have a background of a light grey, so now lets make our comments striped like a lot of wordpress comments. Since the comments are in a list lets take advantage of that.

$('.comments:odd').addClass('stripe');

Now this will apply the “stripe” class to every other comment, making our comments striped =). Haha I like how I doubled this tutorial to tell you how to stripe your comments.

You now know how to find a class, id, or HTML elements using jquery and add a class to it. You can also replace “.addClass” with “.removeClass” to of course remove classes from your selected elements. What about highlighting comments oMouseOver?

$('.comments').mouseOver(function() {
$('this').addClass('hover');
});

“$(’this’) refers back to the last “$(”)” which in this case would be “.comments”, so then the mouse over event adds a function that just says to add the class “hover”. Of course this function would only happen when someone triggers the .mousOver event (when someone hovers over a comment). Obviously the “hover” class would do something like add a new background color or a border, you choose. Although this looks promising we cannot leave the code like this, because then when someone hovers a comment it will not simply remove the “hover” class when they move their mouse away it will stay there. Let’s fix that!

$('comments').mouseOut(function() {
$('this').removeClass('hover');
});

Don’t you just love how easy and short jquery is? The really cool thing is that we are going to chain the .mouseOver and .mouseOut events togethor to make it even shorter. This little trick can be kind of confusing at first, I did not feel comfortable chaining code when I first started jquery. Here it is:

$(".comments").mouseover(function() {
$(this).addClass('hover'); })
.mouseout(function() {
$(this).removeClass("over");
});

Now the code is even shorter! I’m guessing you are pretty much in love with jquery right about now so I will leave you two love birds some alone time. I’ll be back with another fabulous article on learning jquery, and this time it’s on animations and all that fun stuff.

Related Posts

No related posts.
 

13 Comments

  1. ajpiano
    Posted August 23, 2008 at 9:35 am | Permalink

    $(’this’) is just plain wrong.

  2. Posted August 23, 2008 at 9:37 am | Permalink

    Use may instead consider writing your last example as such:

    $(document).ready(function() {
    $(’.comments’).hover(function() {
    $(this).addClass(’hover’);
    }, function() {
    $(this).removeClass(’hover’);
    });
    });

  3. Posted August 23, 2008 at 3:42 pm | Permalink

    if you do not add this code your jquery will be useless.

    i don’t agree with this statement. its just executes the script the moment dom finished loading, which the way we wanted. it is not a necessity.

  4. Posted August 25, 2008 at 2:24 pm | Permalink

    Im ditching mootools for jquery!

  5. Posted August 25, 2008 at 4:47 pm | Permalink

    You’ve got more than a few errors in this post.

  6. Posted August 25, 2008 at 11:49 pm | Permalink

    Thank you! This is a great information. I always wanted to know more about how jquery worked. Nice.

  7. Posted August 26, 2008 at 10:28 pm | Permalink

    Thanks for the comments and I understand the $(’this’) problem, but at this point I sort of forget whether it was $(this) or $(’this’). I know I am not wrong on the “this” expression though.

    Sorry for some of the errors I kind of rushed through writing this because of stupid school stuff.

  8. Posted September 1, 2008 at 12:40 pm | Permalink

    I’ve been using jquery for quite some time now, and the more that you use it, the more you love it.
    Easy to use, and quick learning curve also.
    Great tutorial, shows how easy it is to use.

  9. Posted February 10, 2009 at 11:33 am | Permalink

    Ive been using jquery in that too much some happening now, and the additional that you benediction it, the additional you will it. Easy to use, besides nimble break curve also.Great tutorial, shows how manageable sound is to use.

  10. Posted March 2, 2009 at 12:22 pm | Permalink

    I always wanted to know more about how jquery worked. Thank you for this great information.

    kind regards.

  11. basiL
    Posted April 23, 2009 at 6:30 am | Permalink

    Thanks for the fantastic post. Article was so simple to understand a newbie like me. Both article and comments helped me a lot. I was fed up with copy/paste 1 or 2 times. Thanks a lot.

  12. Posted April 24, 2009 at 3:23 pm | Permalink

    Yeah, I think that Jquery is a lot easier to grasp than mootools, and I just started learning both of them today. I never used much javascript in any of my websites before but Im going to start now hopefully.

  13. Posted September 28, 2009 at 8:00 pm | Permalink

    Great post, but way too many typos.

4 Trackbacks

  1. By Web Design Basics on August 24, 2008 at 10:40 pm

    [...] Learning Jquery: The Basics [...]

  2. [...] Learning jQuery: The Basics – Cats Who Code [...]

  3. [...] Learning jQuery: The Basics – Cats Who Code Graphic Design: [...]

  4. [...] Learning jQuery: The Basics – Cats Who Code [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting

  • Smashing Network
WordPress Appliance - Powered by TurnKey Linux