/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] getElementsByName problem when element is dynamically created

can anyone explain to me why the code in red wont work? i can create the element, verify that i can reference the object using getElementsBy[B]Tag[/B]Name, and then read its name property that way. but the code in red, which by all logic should return a length of 1, does not. not looking for any workarounds here.

[code]<script>
function add()
{
table = document.createElement(“table”)
table.setAttribute(“border”, “1”)

row = table.insertRow(-1)
cell = row.insertCell(-1)
formEl = document.createElement(“input”)
formEl.setAttribute(“name”, “subject1”)
formEl.setAttribute(“type”, “text”)
formEl.setAttribute(“value”, “subject field”)

cell.appendChild(formEl)

document.getElementsByTagName(“body”)[0].appendChild(table)

inputEls = document.getElementsByTagName(“input”)

alert(inputEls.length)

alert(inputEls[0].name)

[COLOR=”Red”]alert(document.getElementsByName(“subject1″).length)[/COLOR]
}
</script>

<body onload=”add()”>

</body>[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@DokMar 29.2008 — Looks like a bug in IE http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1843487&SiteID=1 ... but aren't name attributes deprecated anyway?
Copy linkTweet thisAlerts:
@FangMar 29.2008 — ... but aren't name attributes deprecated anyway?[/QUOTE]
Not for all elements. http://www.w3.org/TR/xhtml1/#h-4.10
Copy linkTweet thisAlerts:
@Angry_Black_ManauthorMar 29.2008 — thanks. thought i was going absolutely nuts
Copy linkTweet thisAlerts:
@DokMar 29.2008 — @ Fang. Noted.
×

Success!

Help @Angry_Black_Man spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...