Jump to content

JQuery - Add class to body if page has an element with specific class

Recommended Posts

Hi all,

I'm struggling to make this function work and would appreciate any and all help.

I'm creating a site for a client and utilizing SS's portfolio function. I made a custom layout via CSS for the individual portfolio pages, and would like the owner of the site to be able to easily toggle these css modifications on or off on a specific page without having to write any code. 

At the top of the individual portfolio pages, I added a code block that looks like this:

<div class="layout1"></div>

Then, I wrote a jquery script (and have the jquery library referenced already in the sitewide footer injection) and it looks like this: 

<script>
$(document).ready(function() {
    if ($("div").hasClass(".layout1")) {
        $("body").addClass(".layout1mods");
    }
</script>

Essentially, if a page has a div with the class .layout1, I'd like the class .layout1mods to be added to the body of a page (thereby adding all of the other modifications that I've specified in the class .layout1mods.

Please let me know if you have any ideas on how to make this work!

Thank you.

 

 

Link to comment
14 hours ago, GraceS1800 said:

Hi all,

I'm struggling to make this function work and would appreciate any and all help.

I'm creating a site for a client and utilizing SS's portfolio function. I made a custom layout via CSS for the individual portfolio pages, and would like the owner of the site to be able to easily toggle these css modifications on or off on a specific page without having to write any code. 

At the top of the individual portfolio pages, I added a code block that looks like this:

<div class="layout1"></div>

Then, I wrote a jquery script (and have the jquery library referenced already in the sitewide footer injection) and it looks like this: 

<script>
$(document).ready(function() {
    if ($("div").hasClass(".layout1")) {
        $("body").addClass(".layout1mods");
    }
</script>

Essentially, if a page has a div with the class .layout1, I'd like the class .layout1mods to be added to the body of a page (thereby adding all of the other modifications that I've specified in the class .layout1mods.

Please let me know if you have any ideas on how to make this work!

Thank you.

 

 

I may suggest you replace this line

if ($("div").hasClass(".layout1")) {

with 

if ($("body").find("div.layout1").length) {

Greeting, it's BeyondSpace, I am Squarespace dev focus on provide solutions to enhance feature that squarespace.com can't provide.
Feel free to check my current Squarespace Plugins Developement: Enable Pinch/Zoom on lightbox, Delivery Date Picker, Keyword Highlighter
If you find my answer fit your need, let's leave a like or upvote so others with the same issue can find their solution. Thank you

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.