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>
1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

as BoltClock wrote, use php to pass the variable.

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

Comments

-1
<script type="text/javascript">

function hidediv() {

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

    divobj.style.display = 'none';

}

</script>

1 Comment

why did i get -1? My code does exactly what he wants. grumble grumble

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.