I conducted several tests to find out if I can find an effective selector for controls with a prefix of the identifier of the parent controls in Asp.Net
I was looking for this because I had to select Asp controls from external javascript files (I'm tired of using ClientID).
To check, I set up a page with 150 text fields, all with a βfastβ cssclass and a separate identifier, and executed the following code to select the 107th text field.
function testclientInput() { var iterations = 100; var totalTime = 0; // Record the starting time, in UTC milliseconds. var start = new Date().getTime(); // Repeat the test the specified number of iterations. for (i = 0; i < iterations; i++) { // Execute the selector. The result does not need // to be used or assigned to determine how long // the selector itself takes to run. // All tests done in ie6, ie7, ie8, ie9beta, firefox3.6, opera11 & chrome8 // slowest // $('input.speedy[id$=_TextBox107]'); // Quick but only useful if you know the index. //$($('input.speedy')[106]); //$('[id$=_TextBox107]:first'); //$('input[id$=_TextBox107]'); //$.clientID("TextBox107"); //$('[id$=_TextBox107]'); //$('input[id$=_TextBox107]:first'); //$($('[id$=_TextBox107]')[0]); // Fastest //$($('input[id$=_TextBox107]')[0]); } // Record the ending time, in UTC milliseconds. var end = new Date().getTime(); // Determine how many milliseconds elapsed totalTime = (end - start); // Report the average time taken by one iteration. alert(totalTime / iterations); };
This is the best I've come up with. $($('input[id$=_TextBox107]')[0]); . The results were unexpected .... I expected with the :first selector to match my version, but it reported much slower results.
Can anyone think of how to optimize this?
source share