Retrieve form's "name" attribute in Javascript whereas an input exists with name "name"
12,879
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.
Author by
Frédéric Camblor
Updated on June 05, 2022Comments
-
Frédéric Camblor almost 2 years
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
-
Michael Mao over 13 yearsSo, the question is, which name attribute you are trying to get? The one for the form? Or the one for the text input?
-
Frédéric Camblor over 13 yearsPurpose it to retrieve the "myvalue" form's name attribute value
-
Ruan Mendes over 13 yearsInteresting, getAttribute works for me across all browsers. I tried document.getElementById('tsf').getAttribute('name') on google.com and it returned the form's name (f). I thought it may be related to quirks/standards mode, but it worked with all combinations of browser and document modes. Can you post the full html, including the doctype definition?
-
Frédéric Camblor over 13 yearsWhat is your OS ? (I'm under windows 7)
-
Ruan Mendes over 13 yearsI'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.com. 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.
-
-
Dagg Nabbit over 13 yearsooh neat, didn't realize
attributes
had named properties. -
Frédéric Camblor over 13 yearsI'll try this on IE 7 though ;-)