Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show and Hide a DIV based on a variable PHP jQuery

Tags:

jquery

php

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?

like image 555
Jez Avatar asked Mar 07 '26 06:03

Jez


1 Answers

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>
like image 111
adeneo Avatar answered Mar 08 '26 18:03

adeneo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!