2

I want to dynamically tell a javascript which <div> to hide, but I dont know how to send the request to the javascript as it is a client side script.

for eg:

<?
$divtohide = "adiv";
?>
<script language="javascript">
            function hidediv($divtohide) {
            ................
            }
</script>
Starx
  • 77,474
  • 47
  • 185
  • 261
  • possible duplicate of [PHP/Javascript - get php variable within javascript](http://stackoverflow.com/questions/2787978/php-javascript-get-php-variable-within-javascript) – Felix Kling Jun 20 '10 at 11:33

3 Answers3

5

Assuming $divtohide actually contains the ID of a <div> element and not a JavaScript variable name, write your JavaScript function as normal:

function hidediv(divtohide) {
    // Your code may differ here, mine's just for example
    document.getElementById(divtohide).style.display = 'none';
}

And print out the PHP variable only when you're calling it, within a pair of quotes:

hidediv("<?php echo addslashes($divtohide); ?>");

addslashes() ensures that quotes " in the variable are escaped so your JavaScript doesn't break.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
0

as BoltClock wrote, use php to pass the variable.

but you can do it by most simple way, just write hidediv("<?=$divtohide?>")

Simon
  • 22,637
  • 36
  • 92
  • 121
-1
<script type="text/javascript">

function hidediv() {

    divobj = document.getElementById('<?= $divtohide ?>');

    divobj.style.display = 'none';

}

</script>
code_monk
  • 9,451
  • 2
  • 42
  • 41