Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery selector, should I specify type of html tag?

I have many html tags with class = 'class1'. They all happen to be/ and always will be the same type of element. For example input elements.

Is there a performance difference between

$("input.class1")

and

$(".class1")

Thanks!

NOTE: In this case i need information specifically pertaining to IE8, however a mention of a cross browser solution is important, my clients can only use IE8 so the best answer will have the best answer specifically for IE8.

like image 977
kralco626 Avatar asked Jan 04 '11 18:01

kralco626


Video Answer


1 Answers

Yes, there's a performance difference. The latter allows the selector engine to use the native getElementsByClassName method that exists in most major browsers, and should be slightly faster in browsers that don't support that method or querySelectorAll. The former will be faster in browsers that support querySelectorAll but not getElementsByClassName (which is pretty much just IE 8), but likely still not as fast as the latter for your situation.

For your scenario, use $(".class1").

like image 115
Andy E Avatar answered Oct 17 '22 06:10

Andy E