document.getElementsByName problem

S

simon

<html>
<head>
<script language="vbscript">
sub window_onload
msgbox document.getElementsByName("name1").length
end sub
</script>
</head>
<body>
<div name="name1">testDiv</div>
<table id="tbl1" name="name1"><tr><td>table1</td></tr></table>
<table id="tbl2"
name="name1"><tbody><tr><td>table2</td></tr></tbody></table>
</body>
</html>

Why document.getElementsByName("name1") returns always 0 ? I have 3 elements
with name property="name1"

Does anybody know the answer?

Is there any other way to join this 3 elements?

I have about 1000 table elements on the page and I don't want to use
function document.getElementsByTagName.

Thank you,
Simon
 
S

simon

Thank you for your answer.

Even if I execute the msgbox with click event, still the same problem

Thanks,
Simon
 
E

Eliyahu Goldin

Simon,

Could be the elements are not parsed by the browser yet when window's onload
event fires. Just for testing, make an onclick event for any of the elements
and produce the messagebox there.

Eliyahu
 
B

Bruce Barker

getElementsByName only works for form elements (<input>,<select>, and
<textarea>). to access any html element use getElementById

<html>
<head>
<script language="vbscript">
sub window_onload
msgbox document.getElementByName("name1").innerHTML
end sub
</script>
</head>
<body>
<div id="name1">testDiv</div>
<table id="tbl1" name="name1"><tr><td>table1</td></tr></table>
<table id="tbl2"
name="name1"><tbody><tr><td>table2</td></tr></tbody></table>
</body>
</html>
 
S

simon

Hi,

thank you for your answers.
Is there some other way to get the array of tables if getElementsByName
doesn't work for tables?

regards,
Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top