最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to get input class value by javascript - Stack Overflow

programmeradmin8浏览0评论

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 badges
Add a comment  | 

3 Answers 3

Reset to default 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/

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;
发布评论

评论列表(0)

  1. 暂无评论