How to pass the php variable into Javascript..?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gunasegar
    New Member
    • Aug 2010
    • 10

    How to pass the php variable into Javascript..?

    i have php variable which has to be passed inside the javascript...

    this is my code..

    Code:
    <ul class="frnd_lst">
    <?php
    for($i=1; $i<11; $i=$i+1)
    {
    ?>
    <li id="color" class="frnd_lst_li" onmouseover="color_in()" onmouseout="color_out()">
    <div align="left" style="float:left; margin:5px;"><a href=""><img src="Images/9tharasample.png" alt="" /></a></div>
    <ul class="namelist1">
    <div align="left" class="linespace1" style="float:left;"><!------ ----------></div>
    <li class="dob1"><div style="float:left; text-align:right; width:10px;"></div><a href="">Siva Sundharam</a></li>
    <li class="dob"><div style="float:left; text-align:right; width:10px;"></div>(Pondicherry, India)</li>
    <li class="dob"><div style="float:left; text-align:right; width:10px;"></div>
    <a href="">Profile</a> | <a href="">Playlist</a> | <a href="">Friends</a> | <a href="">Quarks</a></li>
    </ul>
    </li>
    <?php } ?>
    </ul>
    here the javascript
    function color_in()
    {
    document.getElementById("color").styl… = "#DDDBD7";
    //document.getElementById("color").sr… = "loading.gif";
    //document.getElementById("list").sty… = "#DDDBD7";
    }
    function color_out()
    {
    document.getElementById("color").styl… = "#FFFFFF";
    //document.getElementById("list").sty… = "#FFFFFF";
    }
    Now I need to pass the $i variable in js to make that every element of li shows the on mouseover property..
    Last edited by Dormilich; Aug 30 '10, 05:44 PM. Reason: Please use [code] tags when posting code
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Please use [code][/ code] tags for when you post code.

    PHP can pass variables to Javascript like this:
    Code:
    <?php $id = "color"; ?>
    <html>...
    function color_out()
    {
    document.getElementById(<?php echo $id; ?>).styl... = "#FFFFFF";
    }...
    Make sense? You should be able to adapt that into your code.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      comment on your HTML. <div>s are not allowed inside <ul>/<ol> (ok, anything besides <li> is not allowed)

      use a validator to check your HTML code often.

      Comment

      Working...