Reputation: 6810
I am wondering just how come the following is giving me hassle, it looks straight forward but I am not sure whats happening.
So I have a input box and I want a user to fill it in, every time i run the following
i get unidentified and I am not sure why.
<input type="numbers" name="price" class="sell" placeholder="$0.00">
Am using the following javascript
var price = document.getElementsByName("price")
alert(price.value);
Upvotes: 2
Views: 94
Reputation: 30187
var price = document.getElementsByName("price")[0]; alert(price.value);
document.getElementsByName will give you HTMLCollection (kind of array) as a return type. This is because a number of elements in your dom may share the same name. So if you have number of checkboxes having same name you can refere each checkbox with the array index.
Upvotes: 4
Reputation: 943537
You have several problems.
The one causing your problem is that getElementsByName
returns an HTMLCollection (which is like an array), not a single element. You need to pull the first item off it before trying to access its properties.
var price = document.getElementsByName("price")[0];
Second, numbers
it not a type of input. number
(singular) is.
Third, if you give the example $0.00
then you are informing people that you expect them to type a value starting with a $
sign. You can't have a $
sign in a number.
Upvotes: 4