Jez
Jez

Reputation: 260

Show and Hide a DIV based on a variable PHP jQuery

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?

Upvotes: 3

Views: 6118

Answers (3)

donlaur
donlaur

Reputation: 1287

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();
    }
});

Upvotes: 1

adeneo
adeneo

Reputation: 318372

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>

Upvotes: 3

Tamil Selvan C
Tamil Selvan C

Reputation: 20239

Change to

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

instead of

var show=('#showhidediv');

Upvotes: 2

Related Questions