3

I'm trying to Hide/Show a DIV based upon a Variable. This is what I have so far.

I POST the variable into an INPUT

<input type="text" id="showhidediv" name="showhidediv" value="<?php echo $ta; ?>">

This is my jQuery

<script type="text/javascript">
$(document).ready(function(){
    var show=('#showhidediv');
    if(show == 2) {
        $('#landlord').show();
    }
    else if(show == 1)
    {
        $('#landlord').hide();
    }
});
</script>

When the variable is posted into the form from another form the DIV doesn't hide.

Where have I gone wrong?

0

3 Answers 3

3

Why do you need javascript for this, just hide the element with CSS based on the PHP variable ?

<input type="text" id="showhidediv" name="showhidediv" value="<?php echo $ta;?>">

<div id="landlord" style="display:<?php echo $ta==2 ? 'block':'none' ?>"></div>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I used this and it works a treat, thanks for the help :-)
2

Change to

var show= $('#showhidediv').val();

instead of

var show=('#showhidediv');

1 Comment

Thanks I tried what you have written and still it doesn't hide once the value in the input is what's needed to hide the DIV
1

Maybe the problem is the value posting in from another form? It sounds like we are missing some other code here.

JsFiddle - http://jsfiddle.net/S5TZv/

Change the value in the HTML in the form from 1 to 2 and run and then back and it works. To do more than this we need how you are submitting the POST and if it is changing anything, when it is submitted and if you need to call the function again if the page is not reloading... if you are using an AJAX call or not to resubmit the POST.

$(document).ready(function(){
    var show= $('#showhidediv').val();
    if(show == 2) {
        $('#landlord').show();
    }
    else if(show == 1)
    {
        $('#landlord').hide();
    }
});

2 Comments

Thanks yes, I see what you mean it works as the code should through jsfiddle, not sure why the POST would affect it when posting the variable into the DIV itself works
You didn't tell us how that POST got there. Just guessing because the full details are not known. The PHP and CSS answer, while it works is a server side solution. Javascript or jQuery a client side solution.

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.