Note that there are some explanatory texts on larger screens.

plurals
  1. POCan anyone beat this jQuery selector?
    primarykey
    data
    text
    <p>I've been running some tests to see if I can find an efficient selector for controls prefixed with the id of parent controls in Asp.Net</p> <p>I've been looking for this as I was to be able to select Asp controls from external javascript files (I'm fed up of using ClientID). </p> <p>To test I set up a page with 150 textboxes all with the cssclass "speedy" and an individual id and ran the following code to select the 107th textbox.</p> <pre><code>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 &lt; 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 &amp; 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); }; </code></pre> <p>This is the best I have come up with. <code>$($('input[id$=_TextBox107]')[0]);</code>. The results have been surprising.... I expected using the <code>:first</code> selector to match my version but it reported much slower results.</p> <p>Can anyone think of a way to optimize this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload