<!DOCTYPE html> <html> <body> <form action="form" onsubmit="return myFunction()"> Enter name: <input type="text" name="fname" id="fname" /> <input type="submit" value="Submit"> </form> <script> function myFunction() { alert(document.getElementById('fname').value); alert(document.getElementsByName('fname')[0].value); return false; } </script> </body> </html>