0

I have been helping my buddy HTML , when I realized that strange thing we did:

<html>
<body>
<input name="test" value="100">  
</body>

</html>

how do I retrive that value in JS. I've tried it;

var n = document.getElementById("test");
alert(n);

why it show "null" ?`enter code here

David R
  • 14,711
  • 7
  • 54
  • 72
Ariöl V
  • 33
  • 6

4 Answers4

0

Add an id attribute:

<html>
    <body>
        <input id="test" name="test" value="100">  
    </body>
</html>

And read value:

var n = document.getElementById("test").value;
Bartek Fryzowicz
  • 6,464
  • 18
  • 27
0

var n = document.getElementById("test").value;
alert(n);
<input id="test" value="100">  

Use id attribute and .value to fetch its value.

void
  • 36,090
  • 8
  • 62
  • 107
0

If you must use name instead id:

var n = document.getElementsByName("test")[0].value;
alert(n);
<input name="test" value="100">
Arkej
  • 2,221
  • 1
  • 14
  • 21
0

var n = document.getElementById("test");
alert(n.value);
<input type="text" id="test" value="1000" />
Vilas Kumkar
  • 1,390
  • 9
  • 18