1

How to dynamically add :checked:nth-of-type selector to every checkbox with native JavaScript?

I tried this. HTML

<input type="radio" name="slider" title="slide2" class="slider-nav"/>
<input type="radio" name="slider" title="slide3" class="slider-nav"/>

And static selectors in CSS

.slider-nav:checked:nth-of-type(1) ~ .slider-inner {
      left: 0%;
    }

.slider-nav:checked:nth-of-type(2) ~ .slider-inner {
      left: -100%;
    }

.slider-inner {
    position: absolute;
    top: 0;
    left: 0;
}

My idea is add slider-nav:checked:nth-of-type(NN) ~ .slider-inner dynamically with JS

So, on JacaScript file I have next code:

window.addEventListener("load", (e) => {
    let num = document.getElementsByClassName('slider-nav');
    for (let i = 0; i < num.length; i++) {

        let newClass = 'slider-nav' + ':checked:nth-of-type(' + (i + 1) + ')';
        console.log('newClass: ' + newClass);
        // HOW TO ADD CLASS TO STYLESHEET NOW?
    }
});

And here I don't know how to add properties to the class and how to add new class to StyleSheet? And yes, I'm beginner in JS.

5
  • 3
    They're pseudo-classes. You dont add them directly, they match due to the state of the element. In this case surely you just add the checked property? Commented Nov 7, 2019 at 15:45
  • @Jamiec Hi. Thank you for the answer. As you can see form the CSS code, I should change "left" property depends from which radio checked by user. Commented Nov 7, 2019 at 15:50
  • Yes, so depending if its the 1st or second checked item that should work. Commented Nov 7, 2019 at 15:52
  • why would you need to do that? Commented Nov 7, 2019 at 16:11
  • This is a slider. I want automate process. I want add HTML input tags and don't want change anything in CSS file. Shortly, if I adding input, I should to get appropriate number of pseudo classes for each input. And I want to get left: NNN% for every pseudo class. Commented Nov 7, 2019 at 16:18

2 Answers 2

1

Those selectors beginning : are CSS pseudo-classes as such you do not add them directly; they apply depending on the state, or position (etc) of the matched elements.

I have simplified your example below to demonstrate this. Depending on whether the checkbox is in the checked state (:checked) and whether it is first or second (:nth-of-type(1) or nth-of-type(2)) controls what color the span is following them.

.slider-nav:checked:nth-of-type(1) ~ .slider-inner {
  background-color:red
}

.slider-nav:checked:nth-of-type(2) ~ .slider-inner {
  background-color:blue
}

.slider-inner{
  background-color:magenta
}
<input type="radio" name="slider" title="slide2" class="slider-nav" />
<input type="radio" name="slider" title="slide3" class="slider-nav"/>
<span class="slider-inner">Content</span>

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

Comments

0

I found decision. Thanks to @Jamiec

const inputs = document.querySelectorAll('.slider-nav');
for (let i = 0; i <inputs.length ; i++)
{
    inputs[i].addEventListener('click', () => {
        let SI = document.querySelector(".slider-inner");
        SI.style.left = -i*100 + '%';
    })
}

Comments

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.