Reputation: 33
I want to get a value from HTML to my Javascript.
So here's the HTML input section in the body
<input type="text" name="paa" value="0" size="3">
And here's the script part
var paa = document.getElementById("paa").value;
But I only get Null and when I parseInt it I only get NaN.
Surely I'm doing something wrong, but I have no idea what, I've searched for it but other topics seem to work just fine like this or use other stuff like jQuery..
All I want is to get a value from a HTML input to a Javascript variable.
Upvotes: 1
Views: 5659
Reputation: 549
You just forgot the ID attribute in your html
<input type="text" **id="paa"** name="paa" value="0" size="3">
Why not to use jQuery? Will make your life easier when working with HTML DOM.
var value = $('#paa').val();
Upvotes: 0
Reputation: 128786
Your element has a name
of "paa"
, not an id
of "paa"
. Instead of getElementById()
you'll need to use:
document.getElementsByName("paa")
This will return a collection of any elements with that name
attribute. To access each individual one you'll need to use its index. If you only have one "paa"
element on your page, you can use:
document.getElementsByName("paa")[0].value
Upvotes: 3
Reputation: 48818
You simply need to add an ID to your input element, like so:
<input type="text" id="paa" name="paa" value="0" size="3">
This is because you're using getElementById
, but your element doesn't have an ID.
Working example: https://jsfiddle.net/JohnnyWalkerDesign/gs9ek11L/
Upvotes: 1
Reputation: 13
So here's the HTML input section in the body
<input type="text" id="paa" value="0" size="3">
And here's the script part
<script>
var paa = document.getElementById("paa").value;
alert(paa)
</script>
Upvotes: 1
Reputation: 13244
Change
<input type="text" name="paa" value="0" size="3">
to
<input type="text" name="paa" value="0" id="paa" size="3">
Upvotes: 8