Page 1 of 1

IE8, Chrome returns error, see code please

Posted: Thu Sep 10, 2009 5:21 pm
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.

Re: IE8, Chrome returns error, see code please

Posted: Thu Sep 10, 2009 5:36 pm
by Darhazer
getElementsByName() is legal only for input elements.

Re: IE8, Chrome returns error, see code please

Posted: Thu Sep 10, 2009 8:12 pm
by John Cartwright
..and you would preferably use getElementById()

Re: IE8, Chrome returns error, see code please

Posted: Fri Sep 11, 2009 12:34 am
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>

Re: IE8, Chrome returns error, see code please

Posted: Fri Sep 11, 2009 5:23 am
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.

Re: IE8, Chrome returns error, see code please

Posted: Fri Sep 11, 2009 2:01 pm
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)