How to get a selected value from the group of elements with the same name?
How to get a selected value from the group of elements with the same name?
14329-Aug-2023
Updated on 02-Sep-2023
Home / DeveloperSection / Forums / How to get a selected value from the group of elements with the same name?
How to get a selected value from the group of elements with the same name?
Aryan Kumar
02-Sep-2023To get a selected value from a group of elements with the same name, you can use the
querySelector()
method. ThequerySelector()
method takes a selector as its argument and returns the first element that matches the selector.The following code shows how to get a selected value from a group of elements with the same name:
In this code, the
elements
variable is a collection of all the elements that have thename
attribute set tomy_input
. TheselectedValue
variable is the value of the first element in theelements
collection.If there is no selected element, the
querySelector()
method will returnnull
.Here is another way to get the selected value from a group of elements with the same name:
In this code, the
selectedValue
variable is the value of the element that has thename
attribute set tomy_input
.