Tutorials

Latest Word » Tutorials » New to jQuery? Don’t be Scurred…
New to jQuery? Don’t be Scurred…

New to jQuery? Don’t be Scurred…

Tags:

I have been getting familiar with jQuery recently and the more I get into it, the more I’m impressed with it. When I first heard about jQuery I was definitely intimidated by it since I had no previous background with javascript, but I soon realized once you get the basics down its not very hard to pull of some neat effects.

Of course, to master anything it takes dedication and hard work, but for those who are on the sidelines looking in, don’t be scared to get familiar with it. Start today as I share some of the easy tips and tricks that I have picked up along the way. For those who have no idea what jQuery is, go and read up on it here.

1. :first and :last Selectors

This comes in handy especially when creating something that repeats itself and you want to specify a style for the first and last element only. I commonly use this for navigations where I specify the bevel effect, or when I am creating columns and need to ditch the margin on the last column.
:first :last jQuery Tutorial

jQuery
\\Add a Class on the First and Last Element
	$("ul.column li .block:first").addClass("first"); //Add a class name of "first" to the first selector
	$("ul.column li .block:last").addClass("last"); //Add a class name of "last" to the last selector

CSS
.first {background: #ffdbdb;}
.last {
	margin-right: 0;
	background: #c4dcff;
}

References

2. :even and :odd Selectors

This really came in handy when I was designing the resource page on DesignBombs.com. It took me one line of jQuery and the list of articles became much easier to scan. Before learning this technique, I was using PHP to specify my odd/even rows which for me took a lot longer to code.
:even :odd jQuery Tutorial

jQuery
//Specify Even and Odd Selectors
	$("ul li:even").addClass("even");
	$("ul li:odd").addClass("odd");

CSS
.even {
	background: #fff;
	border: 1px solid #ccc;
}
.odd { background: #ccc; }

References

3. Ditch the target=”_blank” – XHTML Compliant

I first learned about this from Jin a little while ago when he wrote an article about how to write XHTML Strict code. I still haven’t implemented this on my own blog, but I have been using it since on my newer projects.

//Open External Links in New Window
	$('a[href^="http://"]')	.attr({ target: "_blank" }); //Makes all links open in a new window
	$('a[href^="http://"].target')	.attr({ target: "_blank" }); //a link with the class of "target" opens in a new window

4. Adding a Class/ID to an Element with toggleClass

As front-end Ninjas, we love having the control of being able to add and remove classes/IDs on our elements when the right scenario comes up. Before I learned jQuery, I always had my programmers take care of these effects, but now I can handle them without bothering anyone. Sweet!
Toggle Class jQuery Tutorial

jQuery
//Toggle Class on Click
	$("ul.col3 li .block").click(function() {
		$(this).toggleClass("active");
	});

CSS
.active {background: #ccc;}

Conclusion

Now that you got a taste of some real simple jQuery tricks, go and experiment with some of your own projects!

Related jQuery Resources

Related Posts

Author Bio

My name is Soh Tanaka and I am a Los Angeles based designer/front-end developer specializing in CSS driven web design with an emphasis on usability and search engine optimization. I also run a CSS Gallery which is updated daily with the best CSS websites from around the world. Come check it out!

You can learn more about me or Twitter  Follow me on twitter for more updates and resources!

Did You Enjoy This Post?

subscribe  Subscribe via RSS or by email to get all upcoming tutorials and articles delivered straight to you.

+ Add Comment50 Peeps Have Spoken Their Minds...

  1. DJ

    The last example will come in handy. I need to learn jQuery so bad… this is a good start for me! thanks

  2. sonichtml

    useful tips

  3. Janko

    Nice reading for beginners!

  4. Namcoy

    Hi, today was first day of my jQuery. it worked! Thank you very much!

  5. Arun Kumar

    Many thanks for encouraging me to learn jQuery, I will definitely give it a try Now, God Bless u

  6. MarcoBarbosa

    Good examples! Thanks for sharing.

  7. Aleksandar Tasevski

    jQuery saved me a lot of javascript coding. I use the “even” and “odd” selectors almost in every list.

  8. Perfectflow

    jQuery saved me a lot of time and money of javascript coding.

  9. Pradeep CD

    Yes… I’m new to jQuery..

    and I will follow your blog…

    thanks

  10. Cerium

    The hack about the target = _blank is not *good*, if we try to validate the generated source, this will fail ;)

    Nice post for beginners by the way.

  11. David Lewis

    I’m moving to jQuery as well (from MooTools and Scriptaculous). I’m mostly moving to it for the AJAX functions. The CSS stuff is cool (first, last, odd, even)… but I’d be careful about making your layouts dependent on them. Always a good practice to make your site degrade gracefully. I’d only them for aesthetic things that you can live without.

    For rows of dynamic thumbnails for instance where I want to kill the right margin on the last item, I just write a simple counter in PHP. Once it hits a defined number… it will append a class of “last” on that element and reset the count to 0. Since this happens server-side, it will work everywhere.

  12. Soh

    Thank you Cerium and David for your tips and heads up!

  13. Neal G

    JQuery grew on me quite a bit as well, mainly because of it’s CSS-friendly selector engine. I was finally able to start enjoying some of the more complex selectors in CSS without having to worry if IE6 would support it.

    It’s also helped me remove a lot of unnecessary classes and IDs from my markup.

  14. dubli

    Thanks for Tips and Tricks.

  15. Jin

    Thanks for referring to my article Soh. Great write up.

  16. Gwóźdź Web designer

    So fast and so efficient lesson.
    Soh Thanks!

  17. Neal

    Great post!

  18. David

    Thanks for this. Will come in very handy indeed! I love the odd even selecters!

  19. Samuel

    Great post…you got me to get over my fear of Jquery and actually learn it. Thank you!

  20. Anton Shevchuk

    Thanks for tutorial. I created slides for beginners – http://anton.shevchuk.name/javascript/jquery-for-beginners-selectors/#slide (article on russian, but not slideshow)

  21. Amit

    Thnx nice article but this Jquery dont seem to be a Cutting-edge Technology

  22. Seb

    Nice examples,

    One question – I want to add the fade effect to the toggleClass() and I used the one explained here (http://docs.jquery.com/UI/Effects/toggleClass) – by providing the second parameter, but for some reason it doesn’t work.
    Here’s my code:

    $(‘.sbm_gard’).click(function() {
    $(this).toggleClass(“cls”, “fast”);
    $(this).next().slideToggle(‘slow’);
    return false;
    }).next().hide();

    This is triggering the accordion and swaps the background image of the button and everything works just fine except the cross fade effect.

    Any idea?

  23. gummisig

    Thank you so much man, this simple little trick helped me out with the work section on my site: http://www.gummisig.com/work …threw out the margin on the .odd div´s :)

    Your right about not being scared of jquery, which I have been. This has given me some more hair on my chest ;)

  24. dannell

    very nice. helped me alot! keep writing! :-)

  25. Television Spy

    wow that’s great, thanks for the cheatsheet.

  26. Caz

    I really like reading your blog, so interesting. I’ve just bought a book on jQuery and plan to get stuck in tomorrow. Thanks for giving me a basic idea of the syntax. It looks very simple, and some very nice effects can be conveyed.

  27. b.althubaiti

    ” I was definitely intimidated by it since I had no previous background with javascript ”

    that’s what I feel right now :)

    thx

  28. ดูหนังออนไลน์

    Thanks for you tutorial.

  29. rajesh

    thank you very much :) u rock

  30. giri

    very nice helped me a lot…

  31. tutorial blog

    thank for code

  32. Rick A Pickett

    you, da, bomb.

  33. Jegan

    thanks for this. but i need the basics. where i can find the beginners tutorials

  34. indialike

    Very nice and useful tutorials to web designers,
    Thanks for posting.

  35. shoaib

    I Think i’ll have to learn jquery …its just so short and sweet

  36. Pradip Jani

    Cool..>!

  37. Jay Tillery

    Awesome primer for newbies getting started in jQuery. It covers most of what I was looking for when I got started. Especially odds and even and first and last selectors. I always wanted to use those straight up with CSS, but as you know IE doesn’t support them.

  38. sanjay

    Awesome, cool tutorials

    Now i badly want to learn jQuery B,coz it is helpful for web designer
    I love to give effects to my web page without using any type of flash element

  39. chinmay

    its a must to learn for web developers ….
    i too want to learn it

  40. alex

    Great work bro.. You r the One..:D

  41. cooljaz124

    This is awesome list.

  42. Cazare

    Very good this tutorial … i have learned a lot new things… thanks

  43. Kapil Kaushik

    This is Nice, It is very helpful for the beginers and Learners

  44. Huw

    Thanks for these simple snippets – I am amazed how simple JQuery can be, even thought I still cannot grasp more than basic JS! Thanks again.

  45. qahar

    usualy i just copy and paste your example to build site.. today, for first time, i try to understand the pondation of the jQuery buiding..
    thanks a lot and can you give more lesson just like this?

  46. Jubayer

    Thank FOr the code…

  47. ดูหนัง

    I am newbie of programer. jQuery is hardest for me. Thank for your sample

  48. Shivkumar

    i have tried these example, it is easy to understand and work.

  49. sagar27

    Hey , I am new to JQuery and this will help me a lot.

Speak Your Mind...

Post HTMLNeed to Post HTML Code? Use Postable to post code friendly html. *Wrap all html code in <pre></pre> tags.
Post HTMLNeed to Keep Updated with Comments? Subscribe to comments now.

CSS Gallery