Seemingly easy javascript question

Al Neri

Diamond Member
Jan 12, 2002
5,680
1
81
Apparently this code only works on the first row labelled hideme. how can i get it to work on every row that has hide me in it? I'm building out a complex listing and will need multiple rows hidden, so separate lines for each row may not work (i.e.
{
document.getElementById("hideme1").style.display = value;
document.getElementById("hideme2").style.display = value;
document.getElementById("hideme3").style.display = value;
document.getElementById("hideme4").style.display = value;
etc. will not do
}

Little help? Thanks!!



<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-...401-19991224/loose.dtd">
<HTML>
<HEAD>
<script type="text/javascript">
function applyDisplay(value)
{
document.getElementById("hideme").style.display = value;
}
</script>
<TITLE>Blank HTML 4 Transitional Page</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</HEAD>
<BODY>
<TABLE BORDER=1>
<TR>
<TD>1</TD><TD>Lorem</TD><TD>Ipsum</TD><TD>dolor</TD><TD>sit</TD>
</TR>
<TR id="hideme">
<TD>2</TD><TD>consectetuer</TD><TD>adipiscing</TD><TD>elit.</TD><TD>Aliquam</TD>
</TR>
<TR id="hideme">
<TD>3</TD><TD>egestas</TD><TD>scelerisque</TD> <TD>ipsum</TD><TD>Donec</TD>
</TR>
<TR>
<TD>4</TD><TD>et</TD><TD>erat</TD><TD>id</TD><TD>massa</TD>
</TR>
</TABLE>
<a href="#" onclick="applyDisplay('none'); return false;">HIDE ROWS</a><br><a href="#" onclick="applyDisplay(''); return false;">SHOW ROWS</a>


</BODY>
</HTML>
 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
id's are suppose to be unique so the first step is change that. You could either have a simple count hideme1, hideme2 etc and have a for loop where you test to see if that element exists and hide it. Another alternative is something like using jQuery library and you could hide them all fairly simply by something like:

$('.hideme).each(function(){ $(this).hide(); });

which would hide all rows which have a classname of 'hideme'
 

clamum

Lifer
Feb 13, 2003
26,256
406
126
Yup, an ID tag is unique so only one element on the page can have it. And jQuery would make this wayyyyy easier to do and it's fairly simple to learn.
 

Leafy

Member
Mar 8, 2008
155
0
0
Originally posted by: Snapster
id's are suppose to be unique so the first step is change that. You could either have a simple count hideme1, hideme2 etc and have a for loop where you test to see if that element exists and hide it. Another alternative is something like using jQuery library and you could hide them all fairly simply by something like:

$('.hideme).each(function(){ $(this).hide(); });

which would hide all rows which have a classname of 'hideme'

... wouldn't this be easier?
$(".hideme").hide();
 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
Originally posted by: Leafy
Originally posted by: Snapster
id's are suppose to be unique so the first step is change that. You could either have a simple count hideme1, hideme2 etc and have a for loop where you test to see if that element exists and hide it. Another alternative is something like using jQuery library and you could hide them all fairly simply by something like:

$('.hideme).each(function(){ $(this).hide(); });

which would hide all rows which have a classname of 'hideme'

... wouldn't this be easier?
$(".hideme").hide();

It would in this case! ;)

I'm just in a habbit of doing more stuff than calling just one method, eg annimation and transitions hence the delgate, but nevermind either way should work ok from the top of my head.
 

suklee

Diamond Member
Oct 9, 1999
4,575
10
81
Originally posted by: sourceninja
jQuery is the way to go. Writing raw javascript is just too frustrating.

What's the best way to start learning jQuery? (I have limited javascript experience)
 

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
I suggest getting a better understanding of ECMAScript before delving into a library, as you'll be able to debug without being completely stuck as libraries abstract a lot of the functionality. Go with O'Reilly Definitive 5th by Flanagan or if you've already programmed before, E262 spec pdf.
 

DannyBoy

Diamond Member
Nov 27, 2002
8,820
2
81
www.danj.me