emenous
emenous

Reputation: 11

How can you add a class to the body tag if the page contains specific text with Jquery?

I'm building an eCommerce site and need to add a class to the body tag if the page contains a specific term, in this case, "Accessories." I've used this code to style the span that it appears in but I would need to wrap the whole page in a class to style certain elements.

$(document).ready(function () {
$("span:contains('Accessories')").addClass("newstyle");

Any suggestions?

Upvotes: 1

Views: 297

Answers (1)

Barmar
Barmar

Reputation: 781120

Use :has() to test if the body contains a specific element that contains Accessories

$("body:has(span[itemprop='name'].newaccessories:contains(Accessories))").addClass("newstyle");

Upvotes: 1

Related Questions