Reputation: 128
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!
Upvotes: 0
Views: 8401
Reputation: 1120
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();"/>
Upvotes: 2
Reputation: 2307
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;
Upvotes: 4