Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting first <select> option as selected
    text
    copied!<p>I'm trying to set the first of a few to set as selected</p> <p>I have</p> <pre><code>&lt;select name="id[21]" class="atributosSort"&gt; &lt;option value="107"&gt;1. Sin adaptadores de video (+$45)&lt;/option&gt; &lt;option value="108"&gt;2. Mini Displayport to DVI (+$112)&lt;/option&gt; &lt;option value="109"&gt;3. Mini Displayport to VGA&lt;/option&gt; &lt;/select&gt; </code></pre> <p>I tried </p> <pre><code>jQuery(".atributosSort")[0].selectedIndex = 0; </code></pre> <p>and</p> <pre><code>jQuery(".atributosSort option:first").attr('selected','selected'); </code></pre> <p>both discussed at <a href="https://stackoverflow.com/questions/1414276/how-to-make-first-option-of-select-selected-with-jquery">How to make first option of &lt;select &gt; selected with jQuery?</a> but when I inspect the DOM, no attribute is added to any </p> <p>I'm using jQuery() because I'm in noConflict mode. Could that be the issue?</p> <p>When I run both attempts to get the selected value I don't get any error in the Script Console</p> <p>Another thing that might be relevant is that I'm sorting the options with this</p> <pre><code>function sortDropDownListByText() { // Loop for each select element on the page. jQuery("select").each(function() { // Keep track of the selected option. var selectedValue = jQuery(this).val(); // Sort all the options by text. I could easily sort these by val. jQuery(this).html(jQuery("option", jQuery(this)).sort(function(a, b) { return a.text == b.text ? 0 : a.text &lt; b.text ? -1 : 1 })); // Select one option. //jQuery(this).val(selectedValue); }); } // Use jQuery via jQuery(...) jQuery(document).ready(sortDropDownListByText); </code></pre> <p>I commented jQuery(this).val(selectedValue); out because I thought that that line was setting some option as selected and then I can't override it but that makes no difference.</p> <p>Any ideas? Thanks</p>
 

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