document.getElementsByName problem

  • Thread starter Thread starter simon
  • Start date Start date
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
 
Thank you for your answer.

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

Thanks,
Simon
 
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
 
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>
 
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
 
Back
Top