user1585111
user1585111

Reputation: 1019

javascript ElementById clarification

My input tag looks like this:

<input type="text" name="kkk" value="dis" disabled="disabled"></input>

This is my JavaScript function

function get(){
    alert(document.getElementById("kkk").value);
}

Though I don't have id for the above tag value gets printed when using getElementById. Can any one explain me the behavior?

Upvotes: 1

Views: 2178

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1073988

My guess is you're using Internet Explorer, which uses name as well as id (in several versions); more here. Microsoft even documents this, but it's out-of-spec behavior which is fixed in IE8 and above (in standards mode).

Upvotes: 3

Quentin
Quentin

Reputation: 943108

You are experiencing error recovery / backwards compatibility features that appear in some browsers (related to a time when giving an element a name or an id created a global variable of the same name), particularly when a standards-mode triggering Doctype is missing. You should not depend on this.

Upvotes: 4

Related Questions