0

Using Bootstrap 4 example html below, how can I extract the text from selected dropdown menu option with JS DOM or Jquery?

<select class="custom-select" id="inputGroupSelect01">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</select>

I know that I can fetch the value but not the text within <option></option> with something like this:

$("#inputGroupSelect01").change(function (){

    alert($(this).val());
});

Let's assume the value is an ID and the text is a column in my database. I need to fetch this information to submit a PUT request with the ID and the text. Thanks for your help!

asleniovas
  • 193
  • 3
  • 21
  • 1
    Possible duplicate of [jQuery Get Selected Option From Dropdown](https://stackoverflow.com/questions/10659097/jquery-get-selected-option-from-dropdown) – sassy_rog Apr 19 '19 at 19:14

1 Answers1

2

Simply add option:selected to your ID in jQuery:

$("#inputGroupSelect01").change(function (){
    alert($("#inputGroupSelect01 option:selected").text())
});
Cybernetic
  • 12,628
  • 16
  • 93
  • 132