On click of radio button, add class to parent

Rob

I have the following code, which when you click a radio button it adds the class checked_radio, when you click on another radio button it removes the class then adds the class to another radio button. That works great.

The second part is that I want to do a similar thing but add the class highlighted to the parent li, it adds it fine but doesn't remove the class when clicking on another radio button. What am I doing wrong?

$('.gfield_radio input').click(function() {
   $('.gfield_radio input').removeClass("checked_radio");
   $(this).addClass("checked_radio");

   $('input.checked_radio').closest('li').removeClass("highlighted");
   $(this).closest('li').addClass('highlighted');   
});
T.J. Crowder

With these lines, you ensure that any previous .gfield input no longer has checked_radio and then add it to the one just clicked:

$('.gfield_radio input').removeClass("checked_radio");
$(this).addClass("checked_radio");

but then, after doing that, you use this to try to remove the old highlight:

$('input.checked_radio').closest('li').removeClass("highlighted");

Of course, by then, you've already updated which radio button has the checked_radio class.

Just change the sequence:

$('.gfield_radio input').click(function() {
   // Out with the old...
   $('input.checked_radio').closest('li').removeClass("highlighted");
   $('.gfield_radio input').removeClass("checked_radio");

   // ...and in with the new
   $(this).addClass("checked_radio")
          .closest('li').addClass('highlighted');   
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Add/Remove class of form element on radio button click

From Dev

Add class to selected radio button

From Dev

Add class to selected radio button

From Dev

add to variable on radio button click php

From Dev

Add/remove class on button click

From Dev

Best way to add class to parent of radio item, remove and add class

From Dev

How to add checked class to a radio button in AngularJS

From Dev

How to add checked class to a radio button in AngularJS

From Dev

add class for selected radio button in thymeleaf

From Dev

Add class to the label for radio button in cakephp 3

From Dev

How to add and remove class on radio button?

From Dev

jQuery: styling and unstyling on click radio button using parent(), next()

From Dev

Updating Radio Button Select on Click Parent Element in Angular

From Dev

How to add style to the parent when radio button is selected

From Dev

When click a link, Need to add checked attribute in radio button

From Dev

Radio button click

From Dev

Radio button click event

From Dev

Radio Button if on click

From Dev

Click Radio Button Selenium

From Dev

Jquery - Add class on click of button / Remove class on click of body

From Dev

Add and remove css class on button click

From Dev

jQuery: Add class if list item has checked radio button inside

From Dev

How do I add class to label when radio button is selected?

From Dev

Add class to radio button in choice list field, orchard cms

From Dev

JavaScript / jquery: add class if a radio button is selected and data attribute qualifies

From Dev

add and remove class when toggling through radio button

From Dev

Trying to use javascript to add unique class when radio button checked

From Dev

Add a Child to the Parent on Button Click Xamarin.forms

From Dev

Click on input field applies a background colour on parent and deselect initial selected radio button

Related Related

  1. 1

    Add/Remove class of form element on radio button click

  2. 2

    Add class to selected radio button

  3. 3

    Add class to selected radio button

  4. 4

    add to variable on radio button click php

  5. 5

    Add/remove class on button click

  6. 6

    Best way to add class to parent of radio item, remove and add class

  7. 7

    How to add checked class to a radio button in AngularJS

  8. 8

    How to add checked class to a radio button in AngularJS

  9. 9

    add class for selected radio button in thymeleaf

  10. 10

    Add class to the label for radio button in cakephp 3

  11. 11

    How to add and remove class on radio button?

  12. 12

    jQuery: styling and unstyling on click radio button using parent(), next()

  13. 13

    Updating Radio Button Select on Click Parent Element in Angular

  14. 14

    How to add style to the parent when radio button is selected

  15. 15

    When click a link, Need to add checked attribute in radio button

  16. 16

    Radio button click

  17. 17

    Radio button click event

  18. 18

    Radio Button if on click

  19. 19

    Click Radio Button Selenium

  20. 20

    Jquery - Add class on click of button / Remove class on click of body

  21. 21

    Add and remove css class on button click

  22. 22

    jQuery: Add class if list item has checked radio button inside

  23. 23

    How do I add class to label when radio button is selected?

  24. 24

    Add class to radio button in choice list field, orchard cms

  25. 25

    JavaScript / jquery: add class if a radio button is selected and data attribute qualifies

  26. 26

    add and remove class when toggling through radio button

  27. 27

    Trying to use javascript to add unique class when radio button checked

  28. 28

    Add a Child to the Parent on Button Click Xamarin.forms

  29. 29

    Click on input field applies a background colour on parent and deselect initial selected radio button

HotTag

Archive