0

I want buttons in a slider to get underlined when a slide is visible. I think I need to check if a data attribute is true, and then add class.

When inspecting my webpage, I find this in properties > dataset: DOMStringMap > isactiveslide: "true"

I need to check if a slide has isactiveslide: "true" (or even data-isactiveslide: "true") and then add class.

I think I am close and have tried these two codes:

jQuery(function () {
  if (jQuery('menu1').attr('isactiveslide') === true) {
    jQuery(this).find("#test1").addClass("underline");
  }
})

and

jQuery('menu1').each(function(){
    if(jQuery(this).attr('isactiveslide')==true())
        jQuery('#test1').addClass('underline');
})

EDIT (added after some great answers and questions)

And here is the section, where the data attribute "isactiveslide" occurs, copied from the page:

<rs-slide data-key="rs-1898" data-title="WORKS" data-in="o:0;" data-out="a:false;" class="menus works1" id="works1" data-originalindex="2" data-origindex="1" data-description="" data-sba="" data-scroll-based="false" style="overflow: hidden; height: 100%; width: 100%; z-index: 20; opacity: 1; visibility: inherit;" data-owidth="300" data-oheight="200" data-rspausetimeronce="0" data-isactiveslide="true"><

So, the next slide which is not yet shown has data-isactiveslide="false". I reckon, identifying "true" is how I can add class.

EDIT May 4th - I think I am close now, but it still does not work.

jQuery('#slide1[data-isactiveslide="true"]')("#slide1-btn").addClass('.underline');

any help is very appreciated!

7
  • 1
    Post js and html will be helpful Commented Mar 26, 2021 at 21:54
  • 1
    You can use the data attributes - developer.mozilla.org/en-US/docs/Learn/HTML/Howto/…. Commented Mar 26, 2021 at 21:55
  • @GeorgeMa I am not sure how to do that. Embarrassing... Commented Mar 26, 2021 at 22:20
  • @Endothermic_Dragon thanks for the link. It is hard for me. I think I understand how to get the attribute, but I have no idea how to check if it belongs to a div in order to add a class to another div... Commented Mar 26, 2021 at 22:29
  • I'm not sure what you mean by that. Are you looking for a sub-div inside the element? If so, then any of the functions used to search for an element would work - querySelector, querySelectorAll, getElementsByTagName, getElementsByClassName, getElementsByName, getElementById, etc. To use it, simply do element.getElem.... Note how this is done on the element, instead of document. Commented Mar 26, 2021 at 23:32

2 Answers 2

1

Can be easily done by css: You need to find the class applied on the active slide and button

rs-slide.menus[data-isactiveslide="true"] .button-class-name-here{
       text-decoration:underline!important;
}

or

Find which slider you are using and on the slide change event of that slider apply the class on the button for styling.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi, only CSS is also what I suspected when reading about it. However, I could not get it to work. I have added the HTML in the original post, perhaps that could clear things?
@Skt is the button inside the slide with data attribute true or outside of that slide?
Oh, you're right! It's outside the slide, in the "global layers" so it's visible on all slides.
I found another attribute and solved it like this, based on your help: rs-module.the-whole-slide[data-slideactive="rs-1899"] .my-button{text-decoration:underline !important;} Thank you!
Glad to hear that :)
0

Try this code:

var $ = document.querySelectorAll.bind(document) //No need for jquery - simply import the function
$(".menu1[data-is-active-slide]").forEach((el, index) => {
  $("#test1")[index].classList.add('underline');
  $("#test1")[index].innerText = "Selected!";
  console.log(1);
})
<div class="menu1" data-is-active-slide='true'>1</div>
<div id="test1"></div>
<div class="menu1" data-is-active-slide='false'>2</div>
<div id="test1"></div>
<div class="menu1">3</div>
<div class="menu2" data-is-active-slide='false'>4</div>
<div class="menu2">5</div>
<div class="menu1" data-is-active-slide>6</div>
<div id="test1"></div>
<div class="menu2">7</div>
<div class="menu1 menu2" data-is-active-slide="true">8</div>
<div id="test1"></div>
<div class="menu1 menu2">9</div>

The beginning declaration of $ is simply defining it since I did not import jQuery.

The next part is where the 'fun' begins. I used $(".menu1[data-is-active-slide]") to select all elements with class menu1 and with the property that data-is-active-slide is present. Then, I simply defined an action inside the function, for the sake of demonstrating that it works.

10 Comments

Hi, this looks wonderful. I have tried a bit, but I cannot combine it with the existing HTML. I have added it to the original post! What do you think?
Try doing this before the forEach: $(".menu1[data-isactiveslide='true']")
I understand! But unfortunately, it doesn't work.
Thanks! It is indeed a great suggestion. It should work. I am quite sure I have done everything right
if you can take a look at the code from May 4th, I'd be grateful! I have added it in the original post, I think it's close; jQuery('#slide1[data-isactiveslide="true"]')("#slide1-btn").addClass('.underline');
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.