开发者

How can I capture a field name (not field value) with Javascript?

I'm trying to find out how I can use javascript to capture the name of a field and assign the name to a variable. I've done开发者_StackOverflow a good amount of searching, but I can only find out how to capture the value of a field and not the name of the field itself.

For example, say I have a asp textbox named "ClientFName". I'd like to use javascript to capture the name of the textbox (ClientFName) and assign the name to a variable.

I'm moderately experienced with javascript but I haven't figured out a way to make this happen. Any help would be great!


You need to find the element in the DOM (which I assume you can do since you can get the value). Then use .name to access its name property, which you can then assign to a variable.

var myName = document.getElementById("myTextbox").name;


By getAttribute() method you can get the attribute value, just check this:

<script> 
function check(){
var v= document.getElementById('mytext').getAttribute('name');
alert(v);
}
</script>

<input type="text" id="mytext" value="test" name="mytext1" />
<input type="submit" name="submit" value="submit"  onclick="check();"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜