how to get the value of dropdown which reside in string in js

The image

How to get the value of an select which i get from innerHTML. like:

 asa=rows[0].children[1].innerHTML;

"<select id="ddlStartMonday" class="htmlComboSearch"><option value="5:00">5:00 am</option><option value="5:30">5:30 am</option><option value="6:00">6:00 am</option><option value="6:30">6:30 am</option><option value="7:00">7:00 am</option><option value="7:30">7:30 am</option><option value="8:00" selected="selected">8:00 am</option><option value="8:30">8:30 am</option><option value="9:00">9:00 am</option><option value="9:30">9:30 am</option><option value="10:00">10:00 am</option><option value="10:30">10:30 am</option><option value="11:00">11:00 am</option><option value="11:30">11:30 am</option><option value="12:00">12:00 am</option><option value="12:30">12:30 am</option><option value="13:00">1:00 pm</option><option value="13:30">1:30 pm</option><option value="14:00">2:00 pm</option><option value="14:30">2:30 pm</option><option value="15:00">3:00 pm</option><option value="15:30">3:30 pm</option><option value="16:00">4:00 pm</option><option value="16:30">4:30 pm</option><option value="17:00">5:00 pm</option><option value="17:30">5:30 pm</option><option value="18:00">6:00 pm</option><option value="18:30">6:30 pm</option><option value="19:00">7:00 pm</option><option value="19:30">7:30 pm</option><option value="20:00">8:00 pm</option><option value="20:30">8:30 pm</option><option value="21:00">9:00 pm</option><option value="21:30">9:30 pm</option><option value="22:00">10:00 pm</option><option value="22:30">10:30 pm</option><option value="23:00">11:00 pm</option><option value="23:30">11:30 pm</option><option value="23:59">12:00 pm</option></select>"

Answers 1

  • You don't need to refer to innerHTML property of the element, since rows[0].children[1] refers to DOM element i.e. select element, You can directly fetch its value using following statements.

    Vanilla JS

    var elem = rows[0].children[1];
    var value = elem.options[elem.selectedIndex].value;
    

    jQuery

    var value = $(rows[0].children[1]).val();
    

Related Articles