jquery - Auto Select From Dropdown If Only Two Options -


i got 2 select dropdowns have 2 options each

the "please select" , "true option".

in case want auto select "true option". value of "true option" won't same "please select" value empty need select 1 not empty value.

if ($('.product-options .inputbox').size() == 2) {    $('.product-options .inputbox').not(.val('')).attr('selected', true);  }
<div class="product-options">    <div id="option-98" class="options_class">      <div>        <strong>size:</strong>        <select id="options98" class="inputbox" name="options[98]" onchange="updateprice();">          <option value="" selected="selected">--- please select ---</option>          <option value="99">small</option>        </select>      </div>    </div>    <div id="option-99" class="options_class">      <div>        <strong>color:</strong>        <select id="options99" class="inputbox" name="options[99]" onchange="updateprice();">          <option value="" selected="selected">--- please select ---</option>          <option value="100">black</option>        </select>      </div>    </div>  </div>

you can use .each function loop through selects.

$(function(){    $("select").each(function(i, e){      var $e = $(e);            if($e.children().length == 2){        $e.find("option[value='']").attr("selected", false);        $e.find("option[value!='']").attr("selected", true);      }    });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="product-options">  <div id="option-98" class="options_class">      <div>          <strong>size:</strong>          <select id="options98" class="inputbox" name="options[98]" onchange="updateprice();">              <option value="" selected="selected">--- please select ---</option>              <option value="99">small</option>          </select>      </div>  </div>  <div id="option-99" class="options_class">      <div>          <strong>color:</strong>          <select id="options99" class="inputbox" name="options[99]" onchange="updateprice();">              <option value="" selected="selected">--- please select ---</option>              <option value="100">black</option>          </select>      </div>  </div>  <div id="option-100" class="options_class">      <div>          <strong>select 3 values:</strong>          <select id="options100" class="inputbox" name="options[100]" onchange="updateprice();">              <option value="" selected="selected">--- please select ---</option>              <option value="100">one</option>              <option value="101">two</option>          </select>      </div>  </div>  </div>


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -