Forum Moderators: open

Message Too Old, No Replies

Looping javascript question

         

ajs83

3:13 am on Apr 20, 2005 (gmt 0)

10+ Year Member



I'm using the code below as template to display the given query on a page featuring ten different results. However whenever I click any box to switch the layer, only the first instance of the code changes instead of the one clicked. How would I change it so that only the one that is clicked changes?

test.js

var current = {cell:0, layerId:'lyr1'}

function changeMe(cell,layerId)
{

if(current.cell)
{
current.cell.style.backgroundColor = "";
current.cell.style.color = "";
}
cell.style.backgroundColor = "#797979";
cell.style.color = "#000000";

document.getElementById(current.layerId).style.display = "none";
document.getElementById(layerId).style.display = "block";

current = {cell:cell, layerId: layerId};
}

html part

<html><head><title>TEST</title>
<style>
#mytable td
{
font-size: xx-small;
font-family: tahoma;
color: #ffffff;
text-align: center;
background-color: #EBEBEB;
width: 99px;
text-decoration: underline;
cursor: pointer; cursor: hand;
}

.nice
{
display: none;
text-align: center;
width: 120px;
height: 60px;
border: solid 1px gray;
}

</style>

<script src="test.js" type="text/javascript"></script>
</head>
<body>

<div class="nice" style="display:block;" id="lyr1">
<img border="0" src="../images/on.gif" width="120" height="60"></div>
<div class="nice" id="lyr2">
Hello</div>
<div class="nice" id="lyr3">
Goodbye</div>

<table id="mytable">
<td COLSPAN="3" height="15" id="nav1" onclick="changeMe(this,'lyr2')">
Text1</td>
<td COLSPAN="4" height="15" id="nav2" onclick="changeMe(this,'lyr3')">
Text2</td>
</table>

</body>
</html>

ajs83

11:32 pm on Apr 20, 2005 (gmt 0)

10+ Year Member



anyone?

Rambo Tribble

1:58 am on Apr 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll either need to pass the id of the current element in your function call, and modify your script to use the passed parameter instead of the stored value of the property, 'current.layerId', or modify the function to allow you to simply pass 'this' with the event.

Another approach is to add an event handler to the target element(s), which then obviates the need for passing 'this' with an element attribute handler.

ajs83

3:15 am on Apr 21, 2005 (gmt 0)

10+ Year Member



You'll either need to pass the id of the current element in your function call, and modify your script to use the passed parameter instead of the stored value of the property, 'current.layerId', or modify the function to allow you to simply pass 'this' with the event.

Another approach is to add an event handler to the target element(s), which then obviates the need for passing 'this' with an element attribute handler.

I know very little about javascript and wouldn't know where to even start looking for what you described (what is used now was a pre-made script. Can you offer any suggestions or even links to good tutorials?

Rambo Tribble

5:49 am on Apr 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, I think I misread the script on the first wash. The formatting is a bit hard to work with. I'll try to get back to you.

ajs83

6:16 am on Apr 21, 2005 (gmt 0)

10+ Year Member



Is there sometype of dynamic tag like a php/mysql query in javascript to handle it on the fly based on db results?

For example, if I was doing it in php I would loop the data for the table name so that each name was based on a db result.

query = mysql_query("select * from blah");
results = mysql_fetch_array($query);
$id = $results[id];

<table id="$id">

?

Rambo Tribble

12:41 pm on Apr 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It will probably be this evening (PDT) before I can take a long enough look to come up with some answers. It should be possible to do what you want.

Rambo Tribble

3:01 am on Apr 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm still not terribly clear on what you want to do. Do you want something like a tabbed interface where clicking on a tab selects a layer to be displayed? Am I to take it you want the information on the layers to come from database queries?

ajs83

5:33 am on Apr 22, 2005 (gmt 0)

10+ Year Member



Right, essentially that is the template that is repeated for each result pulled from the database. It shows up correctly, but when I have more then one result from the db, it only changes the tab on the first stance of the code no matter which instance is clicked.

For example this is a sample of how I would use it (forgive the sloppiness of the code, it is just thrown together to give a general ideal)


$result = mysql_query("select * from users where name=Jim");

//grab all the content
echo "<table border=0><tr>";
while($r=mysql_fetch_array($result)) {
$email=$r["email"];
$reg=$r["reg"];

HTML CODE SNIPPET
<body>

<div class="nice" style="display:block;" id="lyr1">
<? echo "$type";?></div>
<div class="nice" id="lyr2">
<? echo "$email";?></div>
<div class="nice" id="lyr3">
<? echo "$reg";?></div>

<table id="mytable">
<td COLSPAN="3" height="15" id="nav1" onclick="changeMe(this,'lyr2')">
Text1</td>
<td COLSPAN="4" height="15" id="nav2" onclick="changeMe(this,'lyr3')">
Text2</td>
</table>
<?
$count++;
if ($count==3) {
echo "</tr>";
$count=0;
}
}
?>

Rambo Tribble

12:45 pm on Apr 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would probably be more direct to see the code which is output to the browser, because that is where the problem has to lie (though, obviously, it may originate with the server script). If you have a link, you may sticky mail it to me.