Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 911
Frédéric Camblor Avatar asked Oct 14 '10 21:10

Frédéric Camblor


1 Answers

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.

like image 140
lincolnk Avatar answered Oct 01 '22 11:10

lincolnk