简体   繁体   中英

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 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();"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM