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

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

programmeradmin5浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

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)

  1. 暂无评论