tags:

views:

82

answers:

1

Quick question about jQuery and DOM traversal. Look at the code below and tell me why would someone do one over the other? Is there any reason?

this

jQuery("div.section").click(function(){
     jQuery(this).parent().parent().parent().next().find("div.section2").css("color","#fff")
})

instead of this.

jQuery("div.section").click(function(){
     jQuery("div.section2").css("color","#fff")
})
+4  A: 

If there are multiple div's with a class of section2 and you want to target specific one(s) then you'd probably go with the first version - although more than likely that could be "cleaner". The second version will select all div's with a class of section2.

Andy Gaskell
of course. I see it now. Thanks!
madphp