IE8, Chrome returns error, see code please

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Kind-Wizzard
Forum Newbie
Posts: 7
Joined: Thu Sep 10, 2009 11:59 am
Location: Moon

IE8, Chrome returns error, see code please

Post by Kind-Wizzard »

Code: Select all

??
?<img src="img.gif" onclick="oncollapse('okvdes14')"/>
 
<table>
  <tr name="okvdes14" style="display:none;">
      <td>text1</td>
  </tr>
  <tr name="okvdes14" style="display:none;">
      <td>text1</td>
  </tr>
</table>
 
function oncollapse(sid){
    count = document.getElementsByName(sid).length;
    if (document.getElementsByName(sid)[0].style.display == 'table-row') newvis = 'none'; else newvis = 'table-row';    
    for(var i=0; i<count; i++) document.getElementsByName(sid)[i].style.display=newvis;
}
 
It works in Opera and Firefox, but did not in IE, returns error:
document.getElementsByName(...).0.style' - is null or not object

Task is to hide/show table tr-s with the same name (or id). Help me please.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: IE8, Chrome returns error, see code please

Post by Darhazer »

getElementsByName() is legal only for input elements.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: IE8, Chrome returns error, see code please

Post by John Cartwright »

..and you would preferably use getElementById()
Kind-Wizzard
Forum Newbie
Posts: 7
Joined: Thu Sep 10, 2009 11:59 am
Location: Moon

Re: IE8, Chrome returns error, see code please

Post by Kind-Wizzard »

John Cartwright wrote:..and you would preferably use getElementById()
and it's mean, that i can't use this for element's with the same id. Ok.
Can i use this construction?
...
<table>
<div id="someid">
<tr>
</tr>
</div>
</table>
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: IE8, Chrome returns error, see code please

Post by jackpf »

Kind-Wizzard wrote:and it's mean, that i can't use this for element's with the same id. Ok.
You shouldn't have more than one element with the same ID anyway. That's the whole point of an ID, that it's unique.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: IE8, Chrome returns error, see code please

Post by Darhazer »

Using a framework like jQuery will make the task a lot easier.
You can get elements by tagName and then check an attribute, such name or className or something other, in a loop.
Keep in mind that with restoring table-row you can have problems in IE 6, maybe in newer too.
If so, try using '' (reset to default) instead of table-row... there were such problem few months ago with one project, but I can check details only in the office (PM me, if you have such problem and '' does not resolve it)
Post Reply