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

Retrieve form's "name" attribute in Javascript whereas an input exists with name "name&qu

programmeradmin5浏览0评论

I have something like this HTML structure :

   <form name="myvalue" id="hello">
      <input type="text" name="name" />
    </form>

I'd like to retrieve the form's name attribute in Javascript, with a cross browser solution.

Obviously,

document.getElementById("hello").name 

won't work because it will return the corresponding input object.

Under chrome, following code works, but I didn't succeeded to find the equivalent for Internet Explorer 8

document.getElementById("hello").getAttribute("name")

Thanks in advance !

Frédéric

I have something like this HTML structure :

   <form name="myvalue" id="hello">
      <input type="text" name="name" />
    </form>

I'd like to retrieve the form's name attribute in Javascript, with a cross browser solution.

Obviously,

document.getElementById("hello").name 

won't work because it will return the corresponding input object.

Under chrome, following code works, but I didn't succeeded to find the equivalent for Internet Explorer 8

document.getElementById("hello").getAttribute("name")

Thanks in advance !

Frédéric

Share Improve this question edited Oct 14, 2010 at 21:55 Dagg Nabbit 76.8k19 gold badges114 silver badges141 bronze badges asked Oct 14, 2010 at 21:50 Frédéric CamblorFrédéric Camblor 1,3762 gold badges13 silver badges22 bronze badges 5
  • So, the question is, which name attribute you are trying to get? The one for the form? Or the one for the text input? – Michael Mao Commented Oct 14, 2010 at 21:57
  • Purpose it to retrieve the "myvalue" form's name attribute value – Frédéric Camblor Commented Oct 14, 2010 at 22:25
  • Interesting, getAttribute works for me across all browsers. I tried document.getElementById('tsf').getAttribute('name') on google. and it returned the form's name (f). I thought it may be related to quirks/standards mode, but it worked with all binations of browser and document modes. Can you post the full html, including the doctype definition? – Ruan Mendes Commented Oct 14, 2010 at 22:28
  • What is your OS ? (I'm under windows 7) – Frédéric Camblor Commented Oct 14, 2010 at 22:32
  • I'm using XP, and I just tired it under my Windows 7 and it was fine. Did you try running the line I gave you from google.. I'm mentioning this because I ran across problems with using the attributes collection and set/getAttribute worked for me without the need for a wrapper function to normalize across browsers. – Ruan Mendes Commented Oct 14, 2010 at 23:49
Add a ment  | 

2 Answers 2

Reset to default 12

I think this oughtta work

document.getElementById("hello").attributes["name"].value;

tests ok in IE8, which is all I have. you might have to do some browser checking and pick your approach as needed.

edits: actually, your example works fine for me in IE8 too. but not ie7.

Try this:

function getFormName (formElement) {
  if (!formElement) return;
  var a=formElement.attributes;
  for (var i=a.length; i--;) {
    if (a[i].name=='name') return a[i].value;
  }
}

Not sure if it will work in IE. Should work everywhere else.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论