Get the value of the first element (index 0) in a form:
var x = document.getElementById("myForm").elements[0].value;
Click button to display the value of the first element in the form.
<!DOCTYPE html> <html> <body> <form id="myForm" action="/action_page.php"> First name: <input type="text" name="fname" value="CSS"><br> Last name: <input type="text" name="lname" value="HTML"><br> <input type="submit" value="Submit"> </form>//from w w w .ja va2 s .c om <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myForm").elements[0].value; document.getElementById("demo").innerHTML = x; } </script> </body> </html>