Help with Javascript on my wordpress page

Joined
Aug 26, 2023
Messages
1
Reaction score
0
I have a wordpress website where in one page i have several member profiles that appear on different sections of the page. The theme I'm using uses CMSMasters Content Composer as the page builder. They have a Profiles area where i can create different profiles with their description. Each profile also as a subtitle that is going to show on the front-end every time i use the profile element on the page builder.

There is a page where i use several times this profiles and my objective is for a specific profile to have different subtitles in each section. There is three sections that i would like to change the subtitle of a specific profile. The default subtitle for the profile i want to change is 'Direção Artística'.

I've added class names for each of the profiles elements and i'm trying to create code that will change the subtitle depending on the section.

I'm using a code snippet to add the code to the page.

Code:
document.addEventListener('DOMContentLoaded', function() {

const profile9572 = document.getElementById('post-9572');

if (profile9572) {
const subtitle = profile9572.querySelector('.pl_subtitle');
const section = profile9572.closest('.cmsmasters_profile');

console.log(section); // Add this line to check the value of 'section'
console.log('Class list', section.classList);

if (section.classList.contains('escolaprofiles')) {
subtitle.textContent = 'Contemporâneo e Dança Inclusiva';
} else if (section.classList.contains('somaprofiles')) {
subtitle.textContent = 'Dança Inclusiva';
} else if (section.classList.contains('corpoprofiles')) {
subtitle.textContent = 'Direção Artística';
}
}
});


So, the code works until it reaches the class 'escolaprofiles' after that it doesn't go the the next conditional where it should find the class name somaprofiles and change the subtitle of the 'profile9572'
In the front-end you can see that it changed from 'Direção Artística' to 'Contemporâneo e Dança Inclusiva' but it does't go the the next condition.
there is no typos, if i do a 'console.log('Class list', section.classList);' you can see that it calls the classes correctly but i don't understand why it's not going to the next condition.
URL where you can see this live: https://www.dancaleiria.pt/quemsomos/#equipa

Please i would love advice on what I'm doing wrong.
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
Check this

1. Consider 1 of 3 possibilities.
In that syntax, if first condition are true, rest are became ignore.
JavaScript:
if (x == 1) {
 // first possibility
} else if (x == 2) {
 // second possibility
} else if (x == 3) {
 // third possibility
}

try
2. Every single possibility (just separate if statements)
JavaScript:
if (x == 1) {
 // first possibility
}
if (x == 2) {
 // second possibility
}
if (x == 3) {
 // third possibility
}


BTW,
JavaScript:
if (x == 1) {
 // first possibility
} else if (x == 2) {
 // second possibility
} else if (x == 3) {
 // third possibility
} else {
 // default action
}
all above if statements are false do default action.

JavaScript:
switch (x) {
    case 1:
        // first possibility
        break;
    case 2:
        // second possibility
        break;
    case 3:
        // third possibility
        break;
    default:
        // default action
}
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top