jQuery not calling elements in if statements

jQuery not calling elements in if statements

Hi Everyone,

Haven't some problems. The jQuery isn't working when calling elements placed in conditional statements. My sample code is shown below. This code works beautiful if the if/else conditions aren't there. What is the problem? Is there something wrong with my code? Is there a workaround? I absolutely need to use a conditional statement in my production code.



  1. <?php

    $test = 'true';
    print "
    <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
    <html>
    <head>
    <title>JQ Test</title>
    <script src=\"/reports/js/jquery.js\"></script>
     
    <script>
      $(document).ready(function()
        {
            $(\"#result\").click(function ()
            {
                $(\"#resultAlt\").css(\"background-color\",\"white\");
                $(this).css(\"background-color\",\"yellow\");
                $(\"#altTbl\").fadeOut(\"slow\",ShowResults);
            });
            $(\"#resultAlt\").click(function ()
            {
                $(\"#result\").css(\"background-color\",\"white\");
                $(this).css(\"background-color\",\"yellow\");
                $(\"#accTbl\").fadeOut(\"slow\",ShowAltResults);
            });
        });
        function ShowResults()
            {
                $(\"#accTbl\").fadeIn(\"slow\");
            }
        function ShowAltResults()
            {
                $(\"#altTbl\").fadeIn(\"slow\");
            }

    </script>
    <style>
      p { margin: 8px; font-size:16px; }
      .selected { color:red; }
      .highlight { background:yellow; }
      .tblMain
        {
            width:80%;
            display: none;
        }
    </style>
    </head>
    <body>
     <span id=\"result\"><a href=\"javascript:;\" id=\"test\">Results</a></span>
     <span id=\"resultAlt\"><a href=\"javascript:;\" id=\"test2\">Alternative Results</a></span>
       <br>
        <center>
        ";
        if ($test == 'true')
        {
        
        print "
            <table border=\"0\" cellpadding=\"5px\" cellspacing=\"0px\" id=\"accTbl\" class=\"tblMain\" >
            <tr bgcolor=\"whitesmoke\" class=\"pgTxt\" >
                <td align=\"left\" width=\"50px\">Name:</td>
                <td align=\"left\" title=\"Name\">Test User</td>
            </tr>
            <tr bgcolor=\"whitesmoke\" class=\"pgTxt\">
                <td align=\"left\">Address:</td>
                <td align=\"left\" title=\"Address\">Bollywood</td>
            </tr>
            </table>
        </center>    
        ";
        
        }
        
    else
    {
        print "
        <center>
            <table border=\"0\" cellpadding=\"5px\" cellspacing=\"0px\" id=\"altTbl\" class=\"tblMain\">
            <tr bgcolor=\"whitesmoke\" class=\"pgTxt\">
                <td align=\"left\" width=\"50px\">Name:</td>
                <td align=\"left\" title=\"Name\">Michael J.</td>
            </tr>
            <tr bgcolor=\"whitesmoke\" class=\"pgTxt\">
                <td align=\"left\">Address:</td>
                <td align=\"left\" title=\"Address\">Paris, France</td>
            </tr>
            </table>
        </center>
         ";

    }
        
    print "
    </body>
    </html>
    ";

    ?>

































































































Thanks very much

Maurice J.