...

View Full Version : call a function after <td>



burki
05-24-2012, 11:21 AM
hi
i want to call a javascript function when a <td> load.
i write code but it is not working.
explain any one


<head>
<script type="text/javascript">
function bgclr()
{
if(document.getElementById("ggg"))
{
document.getElementById("ggg").style.background="#cccccc";
}
}
</script>
</head>

<body ><table width="60%" border="0">
<tr>
<td title="ggg" id="foo"><script type="text/javascript">bgclr();</script></td>
<td>&nbsp;</td>
<td>&nbsp;</td></tr></table>
<div id="ggg">some text<div>
<body>



if it is possible then how can i pass the id of <td> which is "foo" to bgcle() function.

burki
05-24-2012, 05:50 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
<style type="text/css"></style>
<script type="text/javascript">
function bgclr(elemID){
if(document.getElementById(elemID)){
document.getElementById(elemID).style.background="green";
}
}
</script>
</head>
<body>
<table width="60%" border="0">
<tr>
<?php for($i=0;$i<=4;$i++)
{
?>
<td title="ggg" id="<?php echo $i; ?>"><script type="text/javascript">bgclr(this.id)</script>Some text</td>
<?php

}
?>
</tr>
</table>
</body>
</html>


i want to pass the id of <td> dynamically not statistically.
as there will be five <td> with different id.
is there any way to do so.

felgall
05-24-2012, 08:06 PM
i want to pass the id of <td> dynamically not statistically.
as there will be five <td> with different id.
is there any way to do so.

Well in that case you will not want separate JavaScript for each and can move all the JavaScript together into one separate file (attached at the bottom of the page so all the <td> have already loaded).

You can then set up a loop that takes the content of document.getElementsByTagName('td') and checks which have an id and attach the function to those.


OR you could simply update your PHP to insert the same id in both places in your existing code.

felgall
05-25-2012, 03:13 AM
If the colours of the rows will follow a pattern you can do it entirely with CSS without needing any classes.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum