0

How to set Option of Select HTML tag using data- attribute?

HTML

<select  class="form-control shade_custom_option valid" id="1">
  <option data-isnone="true" selected="selected" value="1">None</option>
  <option data-isnone="false"  value="2">Text1</option>
  <option data-isnone="false" value="3">Text2</option>
</select>

<select  class="form-control shade_custom_option valid" id="2">
  <option data-isnone="true" selected="selected" value="1">None</option>
  <option data-isnone="false"  value="2">Text1</option>
  <option data-isnone="false" value="3">Text2</option>
</select>

JS

$(".shade_custom_option").each(function (index, value) {
  // var optionSelected = $("option:selected", value);
  // How to get option with  data-isnone="true"? and set it as selected option.
});
1
  • 1
    $(".shade_custom_option option[data-isnone=true]").prop('selected', true) Commented Sep 30, 2014 at 13:38

1 Answer 1

2

Set the select value to the value of the matching option:

$(".shade_custom_option").each(function (index, value) {
  $(this).val( $(this).find('option[data-isnone=true]').val() );
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.