Complete novice in JS/jQuery here just trying to do what I can while we fill in a senior dev role at work.
I’m trying to add to the end of these tags "Discount applied in cart". I was going to do this using an :after and "content" tag in css however this tag should only be added to the first 3 tags listed below and not the last 4.
<a class="externalLink">SAVE X%</a>
<a class="externalLink">SPEND $X SAVE X%</a>
<a class="externalLink">EXTRA X% OFF - ONLINE ONLY</a>
<a class="externalLink">BONUS GIFT</a>
<a class="externalLink">BUY 2 FOR $X</a>
<a class="externalLink">COMPLIMENTARY DELIVERY</a>
<a class="externalLink">REDEEM $X</a>
I tried googling a solution and stitched different bits of code together. I have tried the following to at least make the first one work, but unfortunately it didn’t work
$("a.externalLink:contains('SAVE')").html(function(_, html) {
return html.wrapAll('<span class="applied-cart" />');
});
Essentially what I’m hoping to do is change the code to the following
<a class="externalLink"><span class="applied-cart">SAVE X%</span></a>
with the css
.applied-cart:after {
content="Applied in cart";
}
2
Answers
Using your existing
$("a.externalLink:contains('SAVE')")
, you can use.addClass
to apply your class directly to thea
without the need to.wrap
or otherwise add additional HTML.No JS required, just use the power of advanced CSS selectors: