4

I want to add an own class if a list item has an active radio button inside of it.

I tried this code to add a class but I guess there is an error in it:

$('input').change(function() {
  var check = $('.wc_payment_method').map(function(e) {
    return $(this).find('input[type="radio"]').is(':checked')
  }).get()

  $('.wc_payment_method').addClass('active');
})
.wc_payment_method {padding: 10rem;}
.active {background-color: #eee;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<ul>
  <li class="wc_payment_method payment_method_paypal">
    <input id="payment_method_paypal" type="radio" class="input-radio" name="payment_method" value="paypal">
    <label for="payment_method_paypal">PayPal</label>
  </li>

  <li class="wc_payment_method payment_method_creditcard">
    <input id="payment_method_creditcard" type="radio" class="input-radio" name="payment_method" value="creditcard">
    <label for="payment_method_creditcard">Credit card</label>
  </li>
</ul>

6
  • 1
    $('.input-radio:checked').each(function() { $(this).parent().addClass('active')}); Commented Apr 4, 2020 at 11:34
  • thanks! I tried it but it has no effect :-/ Commented Apr 4, 2020 at 11:36
  • Please edit the question and add live code snippet by clicking on <> menu. Commented Apr 4, 2020 at 11:37
  • done, is this what you needed? Commented Apr 4, 2020 at 11:39
  • Yes. Also add the corresponding CSS class active in the snippet. Commented Apr 4, 2020 at 11:41

1 Answer 1

3
  • Loop your inputs using .each()
  • go for $(this).closest('selector') to target the closest selector
  • use .toggleClass('className', Boolean) to toggle the class.

const $inp = $('[name="payment_method"]');

$inp.on("change", function() {
  $inp.each(function() {
    $(this).closest('.wc_payment_method').toggleClass('active', this.checked);
  });
});
.active { background: gold; }
<ul>
  <li class="wc_payment_method payment_method_paypal">
    <input id="payment_method_paypal" type="radio" class="input-radio" name="payment_method" value="paypal">
    <label for="payment_method_paypal">PayPal</label>
  </li>

  <li class="wc_payment_method payment_method_creditcard">
    <input id="payment_method_creditcard" type="radio" class="input-radio" name="payment_method" value="creditcard">
    <label for="payment_method_creditcard">Credit card</label>
  </li>
</ul>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

or don't use JavaScript at all:

  • Use the :checked pseudo
  • Target the sibling Label element General Sibling Combinator selector ~

/* Style the adjacent LABEL instead */
.input-radio:checked ~ label {
  background: gold;
}
<ul>
  <li class="wc_payment_method payment_method_paypal">
    <input id="payment_method_paypal" type="radio" class="input-radio" name="payment_method" value="paypal">
    <label for="payment_method_paypal">PayPal</label>
  </li>

  <li class="wc_payment_method payment_method_creditcard">
    <input id="payment_method_creditcard" type="radio" class="input-radio" name="payment_method" value="creditcard">
    <label for="payment_method_creditcard">Credit card</label>
  </li>
</ul>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

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

6 Comments

Thanks, I've tried your code but it doesn't show any effect. But also no errors?! CSS is no option because I need to add a style to the parent element.
@Cray strange, I provided you working snippets and works like a charm. I cannot guess.
@Cray This seems to work as you stated in the question. The reason your code does not work is because you're applying class to all elements. If this is not working, please add description of what is happening.
@Cray There's 4 lines of JS, please study it - and try to figure out the needed changes you need to apply to your code. Perhaps it's just matter of changing a selector.
Probably that comment should have been on the question. It means in $('.wc_payment_method').addClass('active');, the selector will select all li elements having this class. This is the reason, the class is applied to all lis irrespective of the radio inside it is checked or not.
|

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.