Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>considering you have a simple select box.. you can use <code>val()</code> to set the options..</p> <pre><code>&lt;select id="selectId"&gt; &lt;option value="0"&gt;Default Value&lt;/option&gt; &lt;option value="1"&gt;1&lt;/option&gt; &lt;option value="2"&gt;2&lt;/option&gt; &lt;/select&gt; &lt;input type="button" id="buttonID" value="change"/&gt; </code></pre> <p>try this</p> <pre><code>$('#buttonID').click(function(){ $('#selectId').val('0'); //value of your default option }); </code></pre> <p><a href="http://jsfiddle.net/RnSwF/" rel="nofollow noreferrer"><h1>fiddle here</h1></a></p> <p>However, if you default option is <code>disabled</code>, then you would need a work around to make the assignment. a small bit like so:</p> <pre><code>$(document).on('click', '#buttonID', function(e) { var d = $('#selectId option:disabled').prop('disabled', ''); $('#selectId').val(0); d.prop('disabled', 'disabled'); }); </code></pre> <p><a href="http://jsfiddle.net/qF4jY/" rel="nofollow noreferrer"><h1>jsFiddle</h1></a></p> <p>Please keep in mind you can change the value of <code>$('#selectId').val(0);</code> to suite your own needs. For instance, if the 3rd option is your default and has a value of 'bob', then you could say <code>$('#selectId').val('bob');</code></p> <p><strong>the answer i provided is just a simplest solution...@SpYk3HH modfied it if incase you default selected was disabled.... and yes @Felix Kling already answered this question in previous post <a href="https://stackoverflow.com/questions/17497662/natively-set-html-select-element-to-its-default-value">here</a> so have a look..anyways thanks guys..:)</strong></p> <hr> <h2>Solution using <em>Attribute</em> via jQuery for Cross Compat</h2> <p>To further upon FelixKling's solution, here is the full jQuery version, using .attr which pulls from the original attributes as opposed to "changed properties" from .prop.</p> <pre><code>$(document).on('click', '#buttonID', function(e) { $('#selectID option').filter(function(i){ return this.hasAttribute('selected') }).prop('selected', 'selected') }); </code></pre> <p><a href="http://jsfiddle.net/VDgPY/" rel="nofollow noreferrer"><h1>jsFiddle Example</h1></a></p> <hr> <h2><a href="http://jsfiddle.net/qTmbY/" rel="nofollow noreferrer">Combo Breaker</a>!</h2> <p>To further solidify the solution, try a combo of the 2 above. This way, regardless of HTML layout, you're bound to reset to default. What I mean is, perhaps you don't have a select box with a set default, but rather option 1 is the default on load. Meanwhile, other selects do have a set default. The following will take care of both problems at the same time.</p> <pre><code>// simply a jQuery 1.7+ way of delegating events to any Element match selector $(document).on('click', 'button', function(e) { // This both selects the first option of a select as well as looks for an option that might have had a default setting var opt = $('select').val(0).children('option').filter(function(i){ return this.hasAttribute('selected') }); // if opt.length is 0, this will do nothing and option 1 is already set, else this will set this default option to selected opt.prop('selected', 'selected'); // if you have an expected event tied to the select's "change" event, you might fire it here, like so: $('select').change(); }); </code></pre> <p>w/Out Comments, nice and short</p> <pre><code>$(document).on('click', 'button', function(e) { var opt = $('select').val(0).children('option').filter(function(i){ return this.hasAttribute('selected') }); opt.prop('selected', 'selected'); }); </code></pre> <p><a href="http://jsfiddle.net/qTmbY/" rel="nofollow noreferrer"><h1>Example 1</h1></a> <a href="http://jsfiddle.net/HUCCV/" rel="nofollow noreferrer"><h1>Example 2 (with change event)</h1></a></p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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