What you could just do was use your PHP for the echo code to trigger the JavaScript variable.
 <script type="text/javascript"> <?php $phpVar = "foo"; echo "var phpVariable = '{$phpVar}';"; ?> </script> 
After the PHP code is parsed and the HTML is sent to the user - everything they saw is the result of a PHP echo -
 <script type="text/javascript"> var phpVariable = 'foo'; </script> 
Now your phpVariable is available for your JavaScript! So you use it, as in any other case -
 $("div."+phpVariable); 
This will lead us to any <div> element with class foo -
 <div class="foo"></div> 
 source share