Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
Old 01-21-2005, 04:08 PM   #1
Member (5 bit)
 
Join Date: Mar 2002
Posts: 26
Using Javascript to create alternating rows in a table

Hello,I'm trying to loop through a table and set a different style for every other row. I can't seem to make it work. The code below highlights every row the same color based on the number of rows entered (i.e. Rows variable). Any ideas?
PHP Code:
for (i=1Rowsi++) {
if (
== 0) {
begRow '<tr class="wr">';
}
else {
begRow '<tr class="ar">';
}


Last edited by Statica; 01-24-2005 at 06:27 PM. Reason: Added code tags
spanky1339 is offline   Reply With Quote
Old 01-21-2005, 09:12 PM   #2
Moderator
Staff
Premium Member
 
Join Date: Aug 2003
Location: Richmond, VA
Posts: 7,835
Can you edit your post to put the entire code within the [ php ] [/ php ] brackets (without spaces)?

kram
__________________
"For today, goodbye. For tomorrow, good luck. And forever, Go Blue!"
University of Michigan President Mary Sue Coleman
kram 2.0 is offline   Reply With Quote
Old 01-22-2005, 10:21 AM   #3
Member (5 bit)
 
Join Date: Mar 2002
Posts: 26
Code for alternate rows

Quote:
Here is some readable code. Rows is the number entered by the user. The wr and ar classes reference the colors in the stylesheet. Thanks.
PHP Code:
for(i=1;i<Rows;i++){
if (
i%2==0){
begRow='<tr class="wr">';
}
else{
begRow='<tr class="ar">';
}

spanky1339 is offline   Reply With Quote
Old 01-24-2005, 01:38 PM   #4
Member (5 bit)
 
Join Date: Mar 2002
Posts: 26
alternate table rows using javascript

I posted my issue last week but the code wasn't displaying correctly because I didn't use the php tags. I'm posting it again because I haven't heard anything. I'm trying to alternate the row colors of every other table row by looping through the number of rows entered by the user. The code I created below highlights all the rows one color, depending on the number I enter. Any ideas?
PHP Code:
var Rows document.DataForm.Rows.value
var 
begRow  '<tr>';
for(
i=0Rowsi++) {
if (
== 0) {
begRow '<tr class="wr">';
}
else {
begRow '<tr class="ar">';
}

spanky1339 is offline   Reply With Quote
Old 01-24-2005, 04:58 PM   #5
Member (8 bit)
 
9600baud's Avatar
 
Join Date: Aug 2002
Posts: 246
dont repost on a question you already made a thread for.


That said, enlighten us with what the DataForm object is and what are you trying to extract out of it? Looks like your Rows variable is trying to get the # of rows?

Also, how are you outputting your HTML? are you doing document.write(begRow)? Theres no point putting your loop because you're overwriting the begRow variable over and over again. The rest of your code's necessary...
9600baud is offline   Reply With Quote
Old 01-24-2005, 06:28 PM   #6
Premium Member
 
Statica's Avatar
 
Join Date: Jun 1999
Posts: 9,231
Threads merged.
Statica is offline   Reply With Quote
Old 01-25-2005, 08:39 AM   #7
Member (5 bit)
 
Join Date: Mar 2002
Posts: 26
I'm actually trying to create a table component in Dreamweaver. I created the DataForm object in html so the user can input the number of rows and colums to create a table. I also want to give the functionality of alternating rows. I created an .html file and a .js file and I reference the .js file within the .html as shown below.
PHP Code:
<html>
<
head>
<
title>Insert Data Table</title>
<
script src="DataTable.js"></script>
</head>
<body>
<form name="DataForm"> 
I got the piece working to display the correct number of rows and columns. I'm outputting the .html by returning the variables in my .js file. The totalRows variable contains the begRow piece that I'm trying to alternate. The Rows variable is trying to get the number of rows entered by the user and alternate the style, either "ar" or "wr".

PHP Code:
return begTable 
         
headers +
         
totalRows +
         
endTable;

spanky1339 is offline   Reply With Quote
Old 01-26-2005, 12:27 PM   #8
Member (8 bit)
 
9600baud's Avatar
 
Join Date: Aug 2002
Posts: 246
Here I did the work for you... I dont use any kinda automatic editor because they're crap. EditPlus is better than Notepad because it highlights tags and stuff for you tho.

Anyways... just tag this at the bottom of your HTML page before the /BODY tag. Or put it in a function in a external file and call it at the end, or whatever etc... etc...
PHP Code:
<SCRIPT>
var 
thetables=document.getElementsByTagName("table");

for(
i=0i<thetables.lengthi++)
{
    for(
x=0x<thetables[i].rows.lengthx++)
    {
        if((
x%2)==0)
            
thetables[i].rows[x].style.backgroundColor="#FF0000";
    }
}
</SCRIPT> 
9600baud is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:16 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2