New to jQuery? Don’t be Scurred…
Tags: Beginner
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.

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.

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!

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
- Web Designer Wall – jQuery Tutorials for Designers
- Queness – Useful and Handy jQuery Tips and Tricks
- Learning jQuery – jQuery for Beginners
- jQuery Essentials Presentation
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
Follow me on twitter for more updates and resources!
Did You Enjoy This Post?
Subscribe via RSS or by email to get all upcoming tutorials and articles delivered straight to you.




+ Add Comment50 Peeps Have Spoken Their Minds...
The last example will come in handy. I need to learn jQuery so bad… this is a good start for me! thanks
useful tips
Nice reading for beginners!
Hi, today was first day of my jQuery. it worked! Thank you very much!
Many thanks for encouraging me to learn jQuery, I will definitely give it a try Now, God Bless u
Good examples! Thanks for sharing.
jQuery saved me a lot of javascript coding. I use the “even” and “odd” selectors almost in every list.
jQuery saved me a lot of time and money of javascript coding.
Yes… I’m new to jQuery..
and I will follow your blog…
thanks
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.
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.
Thank you Cerium and David for your tips and heads up!
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.
Thanks for Tips and Tricks.
Thanks for referring to my article Soh. Great write up.
So fast and so efficient lesson.
Soh Thanks!
Great post!
Thanks for this. Will come in very handy indeed! I love the odd even selecters!
Great post…you got me to get over my fear of Jquery and actually learn it. Thank you!
Thanks for tutorial. I created slides for beginners – http://anton.shevchuk.name/javascript/jquery-for-beginners-selectors/#slide (article on russian, but not slideshow)
This video series is very good for learning jQuery too: http://net.tutsplus.com/articles/web-roundups/jquery-for-absolute-beginners-video-series/
Thnx nice article but this Jquery dont seem to be a Cutting-edge Technology
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?
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 ;)
very nice. helped me alot! keep writing! :-)
wow that’s great, thanks for the cheatsheet.
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.
” I was definitely intimidated by it since I had no previous background with javascript ”
that’s what I feel right now :)
thx
Thanks for you tutorial.
thank you very much :) u rock
very nice helped me a lot…
thank for code
you, da, bomb.
thanks for this. but i need the basics. where i can find the beginners tutorials
Very nice and useful tutorials to web designers,
Thanks for posting.
I Think i’ll have to learn jquery …its just so short and sweet
Cool..>!
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.
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
its a must to learn for web developers ….
i too want to learn it
Great work bro.. You r the One..:D
This is awesome list.
Very good this tutorial … i have learned a lot new things… thanks
This is Nice, It is very helpful for the beginers and Learners
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.
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?
Thank FOr the code…
I am newbie of programer. jQuery is hardest for me. Thank for your sample
i have tried these example, it is easy to understand and work.
Hey , I am new to JQuery and this will help me a lot.
Speak Your Mind...