admin管理员组文章数量:1399930
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!
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!
Share Improve this question asked Sep 30, 2011 at 14:21 XediconXedicon 1282 gold badges4 silver badges9 bronze badges 3- 1 What do you mean by "capture"? The "name" attribute is a property of the DOM element. If your JavaScript code has a reference to the DOM element it just accesses the "name". – Pointy Commented Sep 30, 2011 at 14:23
- Capture in response to what? It being the only field on the page? It being the field clicked on? Something else? – Quentin Commented Sep 30, 2011 at 14:23
- I apologize for not providing enough detail. I'm looking to assign the field name to a variable when the onFocus event occurs. My code does not currently have a reference to the DOM element, that would explain why I wasn't able to see the name property. I'll admit I feel a little silly now, and I need to revisit my solution. thanks for the input! – Xedicon Commented Sep 30, 2011 at 14:28
2 Answers
Reset to default 4You 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();"/>
本文标签: How can I capture a field name (not field value) with JavascriptStack Overflow
版权声明:本文标题:How can I capture a field name (not field value) with Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744177882a2594085.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论