on my page I try to make a separation between bought and unsold products. The bought products uses the CSS class "winning_bid" in a . If this class is on a page I would like to hide another with the class "price". I would like to do something like the following structure in JS.
If CSS class "winning_bid" is on this page{
hide "price"
}else
display "winning_bid"}
Is this possible with JS. I need this case also for buttons.
If CSS class "winning_bid" is in a div{
hide specific button in this div
}else
display button
Do you have any ideas?
3
Answers
If you are willing to support newer browsers, there is the
:has()
pseudo-class (which is relatively widely supported) that you can use:This method has the advantage over JS in the sense that if your page is somehow a SPA or its contents are dynamically updated, you need to use MutationObserver to constantly watch changes to the DOM in order to determine if the element
.winning_bid
has been added to the document.I suppose you can use document.querySelector to check if there are any elements with the "winning_bid" class and get all the elements with the price class using querySelectorAll or getElementsByClassName an example of this would be
I haven’t tested this code but it should work in theory. Let me know about the results 👍
In javascript way you can refer the below solution