JavaScript & jQuery

So I love javaScript, now why do I have to learn jQuery?

Anwser is simple, you dont have to learn jQuery, but truth is learning jQuery will do nothing but make your life eaier in client side development and faster performance as well.

Lets say you have a DIV which contains multiple Anchor Link's. what you want to do is SELECT only a few achors within the DIV and highlight them.

Sure in pure javaScript you would want to get the ID of the DIV and then loop through its A's and then apply the class to required elements.

with jQuery you can do all this with ONE single line.

$("#cars a[type=fav]").addClass("ex");

and HTML was like this...

 <div id="cars">

 <a href="" type="fav">Toyota</a>
 <a href="" type="ex">Honda</a>
 <a href="" type="fav">BMW</a>
 <a href="" type="fr">Acura</a>

 </div>

even within jQuery there were many other ways of doing it.

Amature way #1:

   $("#cars a").each(function() {

    if(this.type=="fav") {
     $(this).addClass("ex")
    }
   })

 

Amature way #2:

   $("#girls a[type=fav]").each(function() {
       $(this).addClass("ex")
   })

This is how you can loop through a form in jQuery

$("form[0] *").each( function() { .... } )

This is how you can Loop through a HTML's SELECT

$("form[0] select[name=mySelect] option").each( function(i) {...} )

within LOOP use this to get the property of current element and use $(this) if you want to futher work on it... like applying a class using jQuery etc... for example...

$("form[0] select option").each(function() { alert(this.value) })

Posted by:

Comments History


by: on
here is my Simple Drop Down menu with jQuery. script src="jquery.js"/script script $(document).ready(function() { $("#nav li").hover(function() { $("ul",this).show(slow) }, function() { $("ul",this).hide(slow) }) }) /script style ul li ul { position:absolute; display:none; list-style:none; } /style ul id="nav" liTVs ul li42 Inch/li liSony Tvs/li /ul /li liAppliances ul liMicrowaves/li liOvens/li /ul /li liFurniture ul liMattress/li liBeds/li /ul /li /ul
 

Name :  

Email :  

Comment Below