By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,043 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,043 IT Pros & Developers. It's quick & easy.

getElementsBName and <table>'s

P: n/a
I relatively new to JavaScript and the DOM....

I have a <table name="xxx"> and I try to a
document.getElementsByName("xxx")

I don't seem to get the <table> returned in the NodeList returned by
getElementsByName

If I do a
nl = document.getElementsByTagName("table")

I can iterate through the resultant NodeList and find node with
nl[i].name = "xxx"

Do <table>'s just not work with getElementsByName ?

Thanks
Mike
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Mike Berger" <mb***************@skypoint.com> wrote in message
news:vp************@corp.supernews.com...
<snip>
Do <table>'s just not work with getElementsByName ?


As TABLE elements are not allowed a NAME attribute (according to the
HTML 4 DTDs) so it is probably unrealistic to expect to be able to
retrieve them from an HTML DOM by name (with getElementsByName). You may
find some browsers that will let you, in the same way that you will find
some browsers that will make all attribute values into named properties
of an element, there will be others that don't. Starting from the basis
of valid HTML is likely to significantly increase the chances of
consistent behaviour from DOM methods.

Richard.
Jul 20 '05 #2

P: n/a
"Mike Berger" <mb***************@skypoint.com> writes:
I relatively new to JavaScript and the DOM....

I have a <table name="xxx"> and I try to a
document.getElementsByName("xxx")
Table tags don't have a "name" attribute, so your "HTML" is not correct
HTML.
I don't seem to get the <table> returned in the NodeList returned by
getElementsByName
Which browser are you using? It seems to be consistent behavior in
the browsers I have tested, so it's probably not important.

Are you writing HTML or XHTML? The method getElementsByName works
differently in HTML and XHTML, but the problem seems to be there
in HTML, which is the more accepting of the two.
If I do a
nl = document.getElementsByTagName("table")

I can iterate through the resultant NodeList and find node with
nl[i].name = "xxx"

Do <table>'s just not work with getElementsByName ?


I don't *think* so. The getElementsByName method finds the DOM
nodes with a name property that has the right value. Tables don't
have a DOM property called "name".

Give the table an id and use getElementById instead.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

P: n/a
try using id instead of name

hope this helps

Simon Christie

"Mike Berger" <mb***************@skypoint.com> wrote in message
news:vp************@corp.supernews.com...
I relatively new to JavaScript and the DOM....

I have a <table name="xxx"> and I try to a
document.getElementsByName("xxx")

I don't seem to get the <table> returned in the NodeList returned by
getElementsByName

If I do a
nl = document.getElementsByTagName("table")

I can iterate through the resultant NodeList and find node with
nl[i].name = "xxx"

Do <table>'s just not work with getElementsByName ?

Thanks
Mike

Jul 20 '05 #4

P: n/a
Oh... duh........

Thanks, my bad

"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:bn*******************@news.demon.co.uk...
"Mike Berger" <mb***************@skypoint.com> wrote in message
news:vp************@corp.supernews.com...
<snip>
Do <table>'s just not work with getElementsByName ?


As TABLE elements are not allowed a NAME attribute (according to the
HTML 4 DTDs) so it is probably unrealistic to expect to be able to
retrieve them from an HTML DOM by name (with getElementsByName). You may
find some browsers that will let you, in the same way that you will find
some browsers that will make all attribute values into named properties
of an element, there will be others that don't. Starting from the basis
of valid HTML is likely to significantly increase the chances of
consistent behaviour from DOM methods.

Richard.

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.