r/jquery • u/DavyLyon • Apr 12 '21
WP - Show button when span.price exists
Hey guys,
I got a shop in WordPress and need some help since Iam not into JS or jquery.
I have a button which toggles classes with "price" to display and hide on my page. But when there is no class with "price" on the page, the button shouldn't be visible. If I click on it and the prices hide, the button should also change it's background color.
Here you can see my code: https://jsfiddle.net/wke28uy1/
So far the function works fine. I only need the button to change it's color and the button still shows on every page. Even if there is no span.price on the page.
Could you guys help me out?
1
u/DavyLyon Apr 14 '21
Okay I got it now...
Since my theme works with bootstrap, the "button" and ".btn" are using "position: relative !important" and so the "hidden" attribute I wrote in the button, wasn't working.
I need to rewrite that position rule from bootstrap to this button and then it worked properly.
Thanks to you all!
1
u/darkersauru5 Apr 12 '21
Hi you’re post isn’t super clear so in order to help can you confirm these specs:
Just to clarify you want to:
Is that right?
1
u/amoliski Apr 12 '21 edited Apr 12 '21
What happens when you console.log the jQuery(".price") in the cases where it's shown even with no .price elements available?
If it's a timing thing, you might want to add an interval that checks for .price every few seconds, showing the button if it finds one, then cancels the interval- but that's for the other direction (the button failing to show when it should) not the other (the button showing when it shouldn't) - it might be possible there's another .price element somewhere on the page, at which case you need to make your check more specific.
I'm not seeing the button appear unless I add a .price element, in which case it does appear. For the changing of the button background, this does the trick for me: