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
  • 75,098
  • 44
  • 181
  • 258
  • 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
  • 665,005
  • 155
  • 1,345
  • 1,328
0

as BoltClock wrote, use php to pass the variable.

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

Simon
  • 21,739
  • 36
  • 90
  • 120
-1
<script type="text/javascript">

function hidediv() {

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

    divobj.style.display = 'none';

}

</script>
code_monk
  • 8,121
  • 2
  • 39
  • 35