Log in

View Full Version : changing table (generated with loop) color



hazee
04-25-2008, 03:21 PM
This is HTML, Javascript and ASP


<html>
<script type="text/javascript">
function check(x)
{
document.getElementById("myTable").bgColor=x
}
</script>
<body>

<%
rs.CursorType = 2
rs.LockType = 3
rs.Open "SELECT * FROM tbl_students", adocon

do while rs.EOF=false
%>

<table id="myTable" width="200" border="1">
<tr>
<td><%=rs.fields(0)%></td>
</tr>
<tr>
<td><input type="checkbox" name="asd" onclick="check(this.value)" value="blue">Select to Print</td>
</tr>
</table>

<%rs.movenext
loop%>

</body>
</html>


This loop will generate as many tables as the number of records filtered by recordset object. If i click any checkbox it changes the background color of the first table generated by loop.

What I want is, that it should change the color of corresponding table. FOr example 3 records filtered, so 3 tables generated. If I click the checkbox in 3rd table, it should only change the color of 3rd table not first.

dsagrera
04-26-2008, 02:14 PM
Try placing:

this.style.backgroundColor='Red'

in the onClick

ooosadface
04-29-2008, 07:00 PM
Part of your problem is 'myTable' is the id for all three tables. You should set a 'RecCount=0' just before the 'Do While' and add a counter (i.e. - RecCount = RecCount + 1) right before the 'Loop' and set the table id's to 'id="myTable_<%= RecCount %>"'.

Now, you have distinct id's and can run your javascript function. You just need to modify the function to accept the number of the id you are wanting to change...


<script type="text/javascript">
function check(x, WhichTable)
{
document.getElementById(WhichTable).backgroundColor=x
}
</script>


... to call it...



onclick="check(this.value, 'myTable_<%= RecCount %>')"


To use the simpler 'onclick' code from dsagrera, you would need to still have distinct table id's and the 'onclick' should be...


onclick="myTable_<%= RecCount %>.style.backgroundColor='blue';"

...or...


onclick="myTable_<%= RecCount %>.style.backgroundColor=this.value;"

pqrs055
05-17-2008, 10:21 AM
Like you to write articles, SEO (http://www.seo358.cn)for you Come on! !

mnop672
05-19-2008, 04:04 AM
Nice topic, nice article.

hazee
05-19-2008, 04:44 AM
Thanks ... a big help.

Going to post another thread. Hopefully someone can answer.