0

I have 2 div's, if one is shown with a class="goodbye" - how do I remove the other div with php?

Or do I have to use jQuery?

<!-- this div should not show if the below class="goodbye" is on the page -->
<div class="hello">
  Hello
</div>

<div class="goodbye">
  goodbye
</div>
user3544484
  • 761
  • 1
  • 9
  • 17

5 Answers5

2

Javascript, not PHP.

if ($('.goodbye').length > 1) {
    $('.hello').hide();
}
rollingBalls
  • 1,808
  • 1
  • 14
  • 25
  • This is not plain Javascript -> it is jQuery. @user3544484 don't forget to inlcude the jQuery library – caramba Aug 30 '14 at 20:20
  • 1
    caramba: Sure, but using js libs in Javascript is still Javascript, so technically I am accurate. – rollingBalls Aug 30 '14 at 20:22
  • 1
    @rollingBalls your answer is good! all I wanted to say is it would be even better if you say it is jQuery so the questioner knows he has to include the jQuery cause it wan't work if he just puts it in ` – caramba Aug 30 '14 at 20:27
  • 1
    @caramba: I agree, I didn't mean to sound offensive, sorry if it sounded like so – rollingBalls Aug 30 '14 at 20:28
2

PHP, being a server-side scripting language, can't manipulate the DOM. If the condition you're using to evaluate the display of your <div>'s is processed server-side, then you could use PHP to echo one <div> or the other. Otherwise, use jQuery or JavaScript to manipulate the DOM client-side.

To answer the direct question. Remove it using PHP:

if($hello)    {
    echo "<div class=\"hello\">Hello</div>";
}   else   {
    echo "<div class=\"goodbye\">goodbye</div>";
}
Will
  • 111
  • 1
  • 8
0

You can't do this with PHP since it is a server side language. Once the page is rendered you'll have to use a client side language. Yes, you can use jQuery(Javascript):

//when to handle..
$("input").click(function() {
    $(".hello").toggle();
    $(".goodbye").toggle();
});

http://jsfiddle.net/kfhb7gzq/

msfoster
  • 2,474
  • 1
  • 17
  • 19
0

Here is a CSS option using the sibling selector. If .hello is sibling to .goodbye: display: none;

.goodbye + .hello {
    display: none;
}

<div class="goodbye">
    goodbye
</div>

<div class="hello">
    Hello
</div>

This solution requires reordering the elements because the sibling selector doesn't select the previous.

Brian Dillingham
  • 9,118
  • 3
  • 28
  • 47
-1

You can't do it in PHP once the page is rendered... Use jQuery instead. There are lots of different ways to do this.

Use .is(":visible")); to see if the class is visible or not.

Try this example:

$('#clickme').hover(function () {
    $('.hello').hide();
    $('.goodbye').show();
    alert($('.goodbye').is(":visible"));
}, function () {
    $('.goodbye').hide();
    $('.hello').show();
    alert($('.goodbye').is(":visible"));
});

JSFiddle Demo

imbondbaby
  • 6,351
  • 3
  • 21
  • 54