Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Unhappy call a function after <td>

    hi
    i want to call a javascript function when a <td> load.
    i write code but it is not working.
    explain any one
    Code:
    <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.

  • #2
    New Coder
    Join Date
    May 2012
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Code:
    <!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.
    Last edited by burki; 05-24-2012 at 05:54 PM.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,641
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by burki View Post
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,641
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If the colours of the rows will follow a pattern you can do it entirely with CSS without needing any classes.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •