Most of the time in JavaScript we want to do following things with Select (or dropdown) list box.

– Get the value of selected option – Get the text of selected option – Get the text of option using its value

We can use jQuery for all of the above tasks. jQuery provide very simple one line solution for all of them. Find below code snippet of all three one by one.

Code Snippet:

// Select list box
<select id="dropdown">
    <option value="0">Sunday</option>
    <option value="1">Monday</option>
    <option value="2">Tuesday</option>
    <option value="3" selected="selected">Wednesday</option>
    <option value="4">Thursday</option>
    <option value="5">Friday</option>
    <option value="6">Saturday</option>
</select>
 
// Get the value of selected option
var selectedvalue = $('#dropdown').val();
alert(selectedvalue); // 3
 
 
// Get the text of selected option
var selectedText = $('#dropdown option:selected').text();
alert(selectedText); // Wednesday
 
// Get the text of option using its option value (eg: 5)
var textThroughValue = $("#dropdown option[value='5']").text();
alert(textThroughValue); // Friday

相关文章:

  • 2021-12-14
  • 2022-12-23
  • 2022-12-23
  • 2021-10-16
  • 2022-02-25
  • 2021-11-22
  • 2021-11-28
  • 2021-09-24
猜你喜欢
  • 2022-01-31
  • 2022-01-15
  • 2021-08-30
  • 2021-10-06
  • 2021-08-28
  • 2022-12-23
  • 2021-09-24
相关资源
相似解决方案