-1

I have a script inside php that will change the display of a div when a checkbox is checked, the checkbox location is different from the place where the script is located.

the snippets of the code is shown below

        if($_POST['test'] == 1)
        {
            echo "<script> document.getElementById('seoTerm').style.display='block';</script>";}

I placed the above code in the head of the current page. When the $_POST['test'] is 1, the display of the div with id seoTerm is still none, instead of block.

What's the problem here ?

8
  • 4
    you need to execute it on a event. because the dom may not be ready when your script is triggering now Commented Jul 18, 2013 at 6:43
  • try <script> instead of <script?> Commented Jul 18, 2013 at 6:44
  • 1
    What's the resulting HTML + JS? Commented Jul 18, 2013 at 6:44
  • @DevZer0 how to execute on an event ? Commented Jul 18, 2013 at 6:47
  • 1
    Is div with the id seoTerm located before or after the script tag? Commented Jul 18, 2013 at 6:49

4 Answers 4

6

It might fire to early in order to manipulate the DOM.

Try:

<script>
window.onload = function(){
    document.getElementById('seoTerm').style.display='block';
};
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

@user2106416 or you could also just move your script tag at a place behind the element with the id seoTerm it depends on what you want to achieve. The onload will be triggered when everything is loaded. If you move the script just behind it the element it will show immediately.
1

There is a mistake in the script tag

change to <script> and not <script?>

3 Comments

Make sure you have an element with id seoTerm
echo "<script>alert('Testing')</script>"; Try this just for testing.
Is javascript enabled in your browser?
0

Use

echo "<script>document.getElementById('seoTerm').style.display='block';</script>";

instead of

echo "<script?> document.getElementById('seoTerm').style.display='block';</script>";

1 Comment

refresh the page, i have edited, not suppose to have '?' , type when copying the code over
0

There are 2 solutions you need either use document.ready event or you can echo this code after the element is displayed.

If you do it before the element may not exsits when JavaScript tries to bind an event to it.

echo "<script>window.onload = function(){ document.getElementById('seoTerm').style.display='block'};</script>"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.