tags:

views:

69

answers:

3

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>
+5  A: 

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
A: 
<script type="text/javascript">

function hidediv() {

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

    divobj.style.display = 'none';

}

</script>
Sean
A: 

as BoltClock wrote, use php to pass the variable.

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

Syom