DCJones
DCJones

Reputation: 3451

Get the value a a selected option and use the returned value in a jQuery function

I am trying to get the value from a selected option to use in a jQuery function, the code that produces the select option is:

<option disabled style="color:#003366" value="">Select wayfinder...</option>
  <?php
    do {
  ?>
  <option id="WFID" value="<?php echo $row_MainBoards['DisplayID']?>"><?php echo $row_MainBoards['DisplayDescription']?></option>
  <?php
  } while ($row_MainBoards = mysqli_fetch_assoc($MainBoards));
    $rows = mysqli_num_rows($MainBoards);
    if($rows > 0) {
      mysqli_data_seek($MainBoards, 0);
      $row_MainBoards = mysqli_fetch_assoc($MainBoards);
    }
  ?>
</select>

And then get the value of the selected option using:

wfid = document.getElementById("WFID");

The issue I have the returned content of "wfid" is;

<option id="WFID" value="1">Main reception No1</option>"

I was expecting a 1, 2 or 3 as the returned value. How can I get this to work?

Upvotes: 0

Views: 26

Answers (1)

bjoernffm
bjoernffm

Reputation: 81

you were very close.

instead of just getting the tag

wfid = document.getElementById("WFID");

you can access the current selected value using the value attribute.

Solution

let selectedValue = document.getElementById("WFID").value;

Upvotes: 2

Related Questions