Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Question about jQuery selector lookup performance.

3:10 pm on May 29, 2012 (gmt 0)

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 0

Hi all,

With my project getting bigger by the minute, i'm worried about inadvertently reusing id's in html elements. So i thought i should fully qualifying a jQuery selector path to make sure i don't access the wrong elements.

Is there a marked performance hit increase when fully qualifying a jQuery selector path then just specifying the element id alone?

$('#workorder-form #sa-table-1 tr #wi-UnitPrice') vs $('#workorder-form #wi-UnitPrice') vs $('#wi-UnitPrice')

I prefer to use the highest level id with the target id - $('#workorder-form #wi-UnitPrice') - because things don't break as easily when your changing mid level id's in the css script.

12:44 am on May 30, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
votes: 0

If jQuery assumes id's are unique, it is likely to reduce $('#workorder-form #sa-table-1 tr #wi-UnitPrice') to $('#wi-UnitPrice'), that is ignore all prior to last hash.

Validating HTML better idea ?

Alternatively you could break the expression up

var wform = $('#workorder-form');
var saTable = wform.find('#sa-table-1');
there may be some time advantage here if there are multiple elements to identify within saTable