Return the value of the name attribute of a drop-down list:
var x = document.getElementById("mySelect").name;
Click the button to return the value of the name attribute of the drop-down list.
<!DOCTYPE html> <html> <body> <form> <select name="selectName" id="mySelect"> <option>Apple</option> <option>Pear</option> <option>Banana</option> <option>Orange</option> </select> </form>// w ww . j a v a 2 s .c o m <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("mySelect").name; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The name property sets or gets the value of the name attribute of a drop-down list.
The name attribute will identify form data after submitting to the server.
The name property accepts and returns a String type value.