How do I target the correct data attribute and child element when dealing will multiple occurrences of the same parent element?

ryanvb92

I have a custom slider element. I want to render many slider elements in the same page but the problem I have is that the dynamic CSS styling targets common attribute names thus if I have two elements #sliderA and #sliderB, the changes that should happen to #sliderB happen to sliderA instead.

I suppose I require JavaScript to target the data-freq attribute of the respective slider element.

https://codepen.io/ryanvb92/pen/poNLQEO

As you can guess, I'm working with someone else's pen. Here is the original pen.

slumbergeist

Css is pretty straight forward

input {
        &:checked {
            + label::after {
                border-width: 4px;
                transform: translate(-50%, -50%) scale(0.75);
            }
  ....

The above rule is used to style a checked input and is working as expected. The only problem is that you are providing same id to multiple input tags.

  <input type="radio" name="smokingFreq" id="1" value="1" required>
        
  <input type="radio" name="drinkingFreq" id="1" value="1" required>

Notice how input elements are sharing same id across different sliders. Duplicating ids is strictly avoided because browser expects them to be unique throughout the document.

Just use unique ids for all inputs. Something like this should fix your issue.

  <input type="radio" name="smokingFreq" id="smokingFreq-1" value="1" required>

  <input type="radio" name="drinkingFreq" id="drinkingFreq-1" value="1" required>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I add a namespace attribute to an element in JAXB when marshalling?

Accessing a data attribute in the first/last child element of a parent element with jQuery

How do I override a parent element's width with an implicit child element's width?

jQuery: How do I target the clicked element when they all share the same CSS class?

How to target element by data attribute using Javascript

How do I toggle the readonly attribute of all child element with jquery

How to get data attribute of element when click on it or it child

How do I append more than one child element to a parent element Javascript

XPath select parent attribute and child element data

How do you disable the set width of parent element for child element?

Vanilla JS event delegation - dealing with child elements of the target element

How to get the attribute of a child element based on the attribute of the parent element

How to get child element of another child of the same parent with jQuery/JS

How do I prevent a child element from inheriting parent element's attached attribute (in notched navigation)?

how do i make the opacity of a child element 100% while the parent element has a different opacity?

How to set child of parent element click event target with meteor

How do I change element value based on the data attribute?

How do I get this child element with overflow auto to show when the parent is overflow hidden?

How do I make a child element toggle when the parent is clicked using jQuery?

How do I change a variable in a child element that has ng-click in the parent element.

How do I increase parent element as child element is increased?

Removing an attribute from a parent element but not a child element

XSD data type of child element depending on parent element's attribute

How can I target a child element within a parent element using selenium in python?

how do i delete child element in nested Json Array and rename Id of child element and merge it to parent element?

How do i perform an operation on a child element and return the parent?

jQuery: how do I move a child element into its 'uncle' element? ie the sibling of its parent?

How to target direct child selector of parent element

How to get child element from parent element having multiple same items?