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>
Javascript, not PHP.
if ($('.goodbye').length > 1) {
$('.hello').hide();
}
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"));
});
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>";
}
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();
});
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.