I have a div, which contains a text input field:
<div id="age" class="fullScreen">
<input type="text" id="age" class="textInput" placeholder="Please enter age..." />
<button type="button" onclick="submitAge()">Submit</button>
</div>
submitAge() looks like this:
function submitAge() {
var ageVal = document.getElementById("age").text;
alert(ageVal);
}
But in Google Chrome and Webkit Nightly I see an alert with the text "undefined" (I can't test in any other browsers because the page contains web-sockets).
The chrome developer tools show this:
I assume that the problem is that the input is not in a form, but I don't want it to be in a form because I will be submitting the data via websockets.
Do I have to put it in a form or is there a better solution?
Thanks.
I have a div, which contains a text input field:
<div id="age" class="fullScreen">
<input type="text" id="age" class="textInput" placeholder="Please enter age..." />
<button type="button" onclick="submitAge()">Submit</button>
</div>
submitAge() looks like this:
function submitAge() {
var ageVal = document.getElementById("age").text;
alert(ageVal);
}
But in Google Chrome and Webkit Nightly I see an alert with the text "undefined" (I can't test in any other browsers because the page contains web-sockets).
The chrome developer tools show this:
I assume that the problem is that the input is not in a form, but I don't want it to be in a form because I will be submitting the data via websockets.
Do I have to put it in a form or is there a better solution?
Thanks.
Share Improve this question asked Jun 7, 2011 at 20:06 JJJollyjimJJJollyjim 6,21719 gold badges58 silver badges79 bronze badges 2 |6 Answers
Reset to default 7You have redefined the id "age". Rename either your div or input and you should be fine.
Try:
function submitAge() {
var ageVal = document.getElementById("age").value;
alert(ageVal);
}
var ageVal = document.getElementById('age').value;
alert(ageVal)
Note the .value instead of text
Change:
var ageVal = document.getElementById("age").text;
to
var ageVal = document.getElementById("age").value;
You <div>
has the id of age. This is going to return either the div or an array of HTMLElements. Get a unique id of the input and you will be golden
Try
document.getDocumentById("age").value;
value
nottext
– David Ly Commented Jun 7, 2011 at 20:13