Skip to content Skip to sidebar Skip to footer

Set Dropdown Selected Value After Loading Dropdown Values In Jquery

I am trying to set selected value in dropdown list when the dropdown is successfully loaded. I am loading dropdown list vaues by ajax or jQuery after that set selected value but s

Solution 1:

Here's a way you can use setInterval to keep checking whether the elements have been populated yet. The first setTimeout function is just simulating taking 3 seconds to add an option to a list.

setTimeout(function() {
  var newOption = document.createElement('option');
  newOption.innerHTML = 'Option Text';
  document.getElementById('list').appendChild(newOption);
}, 3000);

// Do something only after list is populatedvar interval = setInterval(function() {
  if (document.querySelectorAll('#list option').length > 0) {
    console.log('List is definitely populated!');
    clearInterval(interval);
  }
}, 200);
<selectid="list"></select>

Post a Comment for "Set Dropdown Selected Value After Loading Dropdown Values In Jquery"