madphp
madphp

Reputation: 1764

jQuery Traversal Question

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")
})

Upvotes: 1

Views: 170

Answers (1)

Andy Gaskell
Andy Gaskell

Reputation: 31761

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.

Upvotes: 4

Related Questions