2

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

3 Answers 3

9

document.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/

Sign up to request clarification or add additional context in comments.

Comments

2

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>

Comments

0
document.getElementById('mytext').className;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.