Show hide HTML Div based on PHP variable, using javascript

Need to show or hide a DIV with Javascript based off of PHP string variable. Basically, I have a DIV with a lot of information and do not want to show it on the page, if the information supplier with the PHP script has not values. I am testing the value by setting a php variable and want to use javascript to see if there is a string in the php variable and if so, show the div. If the string is empty or blank, then hide the DIV.
LVL 1
Michael CrippsMicrosoft Access & Excel Advanced /SQL Syntax ExpertAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kenfcampCommented:
Do you know the string variable that you want to see returned?
1
leakim971PluritechnicianCommented:
why not using PHP directly ?
old school :
<?php
   if(!empty(thestring)) {
?>
      <div>WE SHOW THE DIV</div>
      <div>WE SHOW THE DIV</div>
      <div>WE SHOW THE DIV</div>
      <div>WE SHOW THE DIV</div>
      <div>WE SHOW THE DIV</div>
      <div>WE SHOW THE DIV</div>
      <div>WE SHOW THE DIV</div>
      <div>WE SHOW THE DIV</div>
      <div>WE SHOW THE DIV</div>
<?php
   }
?>

Open in new window

1
Chris StanyonWebDevCommented:
If your page is PHP (I'm assuming it is if you're checking the variable), then why not just use PHP to hide the DIV. You can either set the div to display:none:

<div style="display: <?php echo $someVar == 'someValue' ? 'block' : 'none' ?>">
    This is the content of the div
</div>

Open in new window

or completely leave it out of the HTML:

<?php if ($someVar == 'someValue'): ?>
<div>
    This is the content of the div
</div>
<?php endif; ?>

Open in new window

2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michael CrippsMicrosoft Access & Excel  Advanced /SQL Syntax ExpertAuthor Commented:
Nice job. Worked perfectly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.