1

I want to make a particular option selected with help of js code.

var selectElement = jQuery('[name="order_status"]').children();
console.log(selectElement);
jQuery(selectElement).each(function(index, item) {
  var match = item.attributes.value.nodeValue;
  if (progress === match) {
    alert(match);
    selectElement.innerHTML = progress;
    //jQuery(this).empty().append(html);
  }
});
<select name="order_status" class="select custom-select">
  <option value="In Progress">In Progress</option>
  <option value="Completed">Completed</option>
  <option value="Rejected">Rejected</option>
</select>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
benvc
  • 14,448
  • 4
  • 33
  • 54
kViN
  • 264
  • 1
  • 2
  • 11
  • 5
    Have you even tried `jQuery('[name="order_status"]').val(progress )` – Satpal Aug 24 '18 at 07:30
  • what is the issue can you explain ? – Narendra Jadhav Aug 24 '18 at 07:32
  • Yes, unlucky it din't worked for me :( – kViN Aug 24 '18 at 07:33
  • i want to select a desired option to be shown in the select element which ever i want – kViN Aug 24 '18 at 07:35
  • You've got a strange mix of DOM and jQuery there. You should consider either ditching jQuery altogether, or use it to its fullest extent, like using the code @Satpal mentions, which will work if the `progress` variable contains a string that exactly matches one of the options' values. – Heretic Monkey Aug 24 '18 at 16:28

0 Answers0