I know jQuery is easy to get the value but how can i use Javascript only to get the value?
This is what I did
<input type="text" class="num" /> <a href="#" onclick="alert(document.getElementsByClassName('num').value);"> click </a>
thanks for help
I know jQuery is easy to get the value but how can i use Javascript only to get the value?
This is what I did
<input type="text" class="num" /> <a href="#" onclick="alert(document.getElementsByClassName('num').value);"> click </a>
thanks for help
Share Improve this question asked Oct 19, 2012 at 3:01 oloolo 5,27115 gold badges60 silver badges96 bronze badges3 Answers
Reset to default 9document.getElementsByClassName
returns an array of elements. You're looking for the first element in that array:
document.getElementsByClassName('num')[0].value;
Demo: http://jsfiddle.net/2vRCU/1/
i think the most common way to do that is give the element a "id" .... at least it's what i did when i was using javascript in old days before jQuery and all other JS frameworks.
<input id='mytext' type="text" class="num" />
and use this to capture:
document.getElementById('mytext');
so it will be:
<a href="#" onclick="alert(document.getElementById('mytext').value);"> click </a>
document.getElementById('mytext').className;