Get selected option value

How to get Selected option value from Dropdown using JavaScript

In this article, we will demonstrate how to get the value of the option selected by a user in tag using JavaScript. The JavaScript app will consist of the following functionalities:

  1. Allow users to select an option from the dropdown.
  2. Submit the selected option.
  3. Get value of the selected option and display it on the screen.

1. Wrap around tag

The first step is to make sure that tag is enclosed by tag for the following reasons:

  1. Multiple inputs can be accessed through a single form element. Hence we only need to use the DOM query selector once for the parent element.
  2. Avoid conflicting inputs with the same name.
  3. Allow form attributes like type, action, name, etc.
 
Your Favorite Super Hero:

2. Declare name for tag

Next, we need to specify the “name” attribute for the tag which allows it to be accessed directly from the parent tag.

Читайте также:  Css grid reverse row

In the below example, we are specifing the name attribute of as “superHero”. The element is accessed through DOM using “document.forms[‘user-form].superHero” .

 

3. Adding value to each tag

Now we are able to access the tag from DOM, hence the option selected from the dropdown needs to be identified. To achieve this, we need to differentiate each option by specifying different strings for the “value” attribute.

In the below example, if user selects “Batman” option from dropdown, the value of “document.forms[‘user-form].superHero” will be “batman“. Subsequently, if the selection is changed to “Spiderman“, the value will be updated to “spideman“.

   

4. JS function to get value selected by user

Once the HTML code is in place, it is time to add JavaScript code for displaying the selected value on the screen.

In the “handleSubmit” function, “event.preventDefault()” avoids default action of reloading the page. Next, the selected value is displayed on screen by assigining string value with “selectedOption.value” for “displayValue.innerText“.

var showValue = document.getElementById("display-value"); // Function to display selected value on screen function handleSubmit(event) < event.preventDefault(); var selectedOption = document.forms["user-form"].superHero; showValue.innerText = `You have selected: $`; >

5. Display selected value on button click

For implementing the functionality to display selected value on button click, we need to create and html element.

 Finally to trigger the “handleSubmit” function on button click, the javascript event listener of type “click” is added.
// Attach function to handle button click submitOption.addEventListener("click", handleSubmit);

Final Solution Code

      
Your Favorite Super Hero:
var submitOption = document.getElementById("submit-option"); var showValue = document.getElementById("display-value"); // Attach function to handle button click submitOption.addEventListener("click", handleSubmit); // Function to display selected value on screen function handleSubmit(event) < event.preventDefault(); var selectedOption = document.forms["user-form"].superHero; showValue.innerText = `You have selected: $`; >

Источник

JavaScript: How to Get the Value of a Select or Dropdown List

Getting the value of a select in HTML is a fairly recurring question. Learn how to return the value and text of a dropdown list using pure JavaScript or jQuery.

Let’s assume you have the following code:

English    

How to get the value of a select

To get the value of a select or dropdown in HTML using pure JavaScript, first we get the select tag, in this case by id, and then we get the selected value through the selectedIndex property.

The value «en» will be printed on the console (Ctrl + Shift + J to open the console).

Getting the value of a select with jQuery

How to get the text of a select

To get the content of an option, but not the value, the code is almost the same, just take the text property instead of value.

The text «English» will be printed on the console (Ctrl + Shift + J to open the console).

Getting the text from a select with jQuery

Complete example

In the code below, when we change the dropdown value, the select value and text are shown in an input field.

     function update() < var select = document.getElementById('language'); var option = select.options[select.selectedIndex]; document.getElementById('value').value = option.value; document.getElementById('text').value = option.text; >update();   

Источник

How to Get the Value of Selected Option in a Select Box

In this tutorial, you will learn how to get the value of the selected option in the select box using jQuery. There are two methods of getting the value of the selected option. You can either select text or find the position of it in a drop-down list by using the option:selected attribute or the val() method in jQuery.

The val() method returns the value of selected attribute value.

let selectedValue = $("#selectVal option:selected").val();

The text() method returns a string of the selected option.

let selectedValue = $("selectVal option:selected").text();

Let’s show what will return these two methods with the following example:

html> html> head> title>Title of the Document title> script src="https://code.jquery.com/jquery-3.5.0.min.js"> script> head> body> form> label>Select Names: label> select class="selectVal"> option value="1">Tom option> option value="2">John option> option value="3">James option> option value="4">Ann option> option value="5">Maria option> select> form> script> $(document).ready(function( ) < $("select.selectVal").change(function( ) < let selectedItem = $(this).children("option:selected").val(); alert("You have selected the name - " + selectedItem); >); >); script> body> html>

The val() method will select the value of the option for example «1», and if you want to get the string «Tom» you can use the text() method.

The text() and val() Methods

The text() method will set or return the text content of the selected elements.

It returns the text content of all matched elements. When it is used to set content, it overwrites the content of all matched elements.

The val() method is an inbuilt method in jQuery, which is used to get the values of form elements (e.g., input, select, textarea). It will return the value of the value attribute of the first matched element.

Using Vanilla JavaScript

To get the value of the selected option in a select box using JavaScript, you can use the value property of the select element. Here’s an example:

html> html> head> title>Title of the Document title> script src="https://code.jquery.com/jquery-3.5.0.min.js"> script> head> body> select id="my-select"> option value="option1">Option 1 option> option value="option2">Option 2 option> option value="option3">Option 3 option> select> script> var selectBox = document.getElementById("my-select"); var selectedValue = selectBox.value; alert(selectedValue); // prints the value of the selected option to the console  script> body> html>

In this example, we have a select box with three options, each with a different value. We’ve used JavaScript to get a reference to the select box using its ID, my-select . We’ve then used the value property of the select box to get the value of the currently selected option. We’ve stored this value in a variable called selectedValue and logged it to the console.

Note that if you want to get the text of the selected option instead of its value, you can use the textContent property of the selected option element. Here’s an example:

var selectBox = document.getElementById("my-select"); var selectedOption = selectBox.options[selectBox.selectedIndex]; var selectedText = selectedOption.textContent; alert(selectedText); // alerts the text of the selected option to the console

In this example, we’ve used the selectedIndex property of the select box to get the index of the currently selected option. We’ve then used this index to get a reference to the selected option element using the options property of the select box. We’ve used the textContent property of the selected option to get its text content and stored it in a variable called selectedText .

And here’s the full example:

html> html> head> title>Title of the Document title> script src="https://code.jquery.com/jquery-3.5.0.min.js"> script> head> body> select id="my-select"> option value="option1">Option 1 option> option value="option2">Option 2 option> option value="option3">Option 3 option> select> script> var selectBox = document.getElementById("my-select"); var selectedOption = selectBox.options[selectBox.selectedIndex]; var selectedText = selectedOption.textContent; alert(selectedText); // alerts the text of the selected option to the console  script> body> html>

Источник

How to Get the Value of Selected Option in a Select Box

In this tutorial, you will learn how to get the value of the selected option in the select box using jQuery. There are two methods of getting the value of the selected option. You can either select text or find the position of it in a drop-down list by using the option:selected attribute or the val() method in jQuery.

The val() method returns the value of selected attribute value.

let selectedValue = $("#selectVal option:selected").val();

The text() method returns a string of the selected option.

let selectedValue = $("selectVal option:selected").text();

Let’s show what will return these two methods with the following example:

html> html> head> title>Title of the Document title> script src="https://code.jquery.com/jquery-3.5.0.min.js"> script> head> body> form> label>Select Names: label> select class="selectVal"> option value="1">Tom option> option value="2">John option> option value="3">James option> option value="4">Ann option> option value="5">Maria option> select> form> script> $(document).ready(function( ) < $("select.selectVal").change(function( ) < let selectedItem = $(this).children("option:selected").val(); alert("You have selected the name - " + selectedItem); >); >); script> body> html>

The val() method will select the value of the option for example «1», and if you want to get the string «Tom» you can use the text() method.

The text() and val() Methods

The text() method will set or return the text content of the selected elements.

It returns the text content of all matched elements. When it is used to set content, it overwrites the content of all matched elements.

The val() method is an inbuilt method in jQuery, which is used to get the values of form elements (e.g., input, select, textarea). It will return the value of the value attribute of the first matched element.

Using Vanilla JavaScript

To get the value of the selected option in a select box using JavaScript, you can use the value property of the select element. Here’s an example:

html> html> head> title>Title of the Document title> script src="https://code.jquery.com/jquery-3.5.0.min.js"> script> head> body> select id="my-select"> option value="option1">Option 1 option> option value="option2">Option 2 option> option value="option3">Option 3 option> select> script> var selectBox = document.getElementById("my-select"); var selectedValue = selectBox.value; alert(selectedValue); // prints the value of the selected option to the console  script> body> html>

In this example, we have a select box with three options, each with a different value. We’ve used JavaScript to get a reference to the select box using its ID, my-select . We’ve then used the value property of the select box to get the value of the currently selected option. We’ve stored this value in a variable called selectedValue and logged it to the console.

Note that if you want to get the text of the selected option instead of its value, you can use the textContent property of the selected option element. Here’s an example:

var selectBox = document.getElementById("my-select"); var selectedOption = selectBox.options[selectBox.selectedIndex]; var selectedText = selectedOption.textContent; alert(selectedText); // alerts the text of the selected option to the console

In this example, we’ve used the selectedIndex property of the select box to get the index of the currently selected option. We’ve then used this index to get a reference to the selected option element using the options property of the select box. We’ve used the textContent property of the selected option to get its text content and stored it in a variable called selectedText .

And here’s the full example:

html> html> head> title>Title of the Document title> script src="https://code.jquery.com/jquery-3.5.0.min.js"> script> head> body> select id="my-select"> option value="option1">Option 1 option> option value="option2">Option 2 option> option value="option3">Option 3 option> select> script> var selectBox = document.getElementById("my-select"); var selectedOption = selectBox.options[selectBox.selectedIndex]; var selectedText = selectedOption.textContent; alert(selectedText); // alerts the text of the selected option to the console  script> body> html>

Источник

Оцените статью