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

Metzed

I have the following HTML radio buttons and I need to add 'checked' as the class of the label when the relevant radio button is selected. The code below works for clicks, but I also need the class added if one of the buttons is selected without clicking, i.e. if the radio button was already selected when the page loaded.

<label><input name="radio" type="radio">One</label><br/>
<label><input name="radio" type="radio">Two</label>

Here's the js:

  $(document).ready(function(){

    $('label').click(function(){ 
        $('label').removeClass('checked'); 
        $(this).addClass('checked');
    });

    });
j08691

Just add:

$('input:checked').parent().addClass('checked');

jsFiddle example

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 jquery when radio button is selected

From Dev

How do I get the text of the radio button selected item?

From Dev

How to add checked class to a radio button in AngularJS

From Dev

Add class to radio's label when selected, and remove the class when it isn't?

From Dev

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

From Dev

How do I add "active" class to dashboard menu button when visiting in CodeIgniter

From Dev

How can I run a function when a radio is selected and I click a button?

From Dev

Add class to selected radio button

From Dev

How to add checked class to a radio button in AngularJS

From Dev

bootstrap: change class of tr in a table when radio button selected

From Dev

How do I get the value of a user-selected radio button in the controller?

From Dev

how to retain radio button selected when i again view same view page?

From Dev

How to align label and radio button?

From Dev

How to add class to button of selected marker

From Dev

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

From Dev

How can I run a function when a radio is selected and I click a button?

From Dev

add class for selected radio button in thymeleaf

From Dev

How do I change the name of a label if a specific radio button is checked?

From Dev

Add class to selected radio button

From Dev

How do I make different panes visible when button selected?

From Dev

Set the class on selected radio button

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

How do I get the correct radio button selected using MVC RadioButtonFor with float values?

From Dev

How to associate a label with the radio button?

From Dev

How to change button color when radio input is selected, but input is wrapped in div before label?

From Dev

How do I manage the layout in android after the Radio button is selected?

From Dev

How do i Get value of selected radio button in angularjs?

From Dev

How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)

Related Related

  1. 1

    Add jquery when radio button is selected

  2. 2

    How do I get the text of the radio button selected item?

  3. 3

    How to add checked class to a radio button in AngularJS

  4. 4

    Add class to radio's label when selected, and remove the class when it isn't?

  5. 5

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

  6. 6

    How do I add "active" class to dashboard menu button when visiting in CodeIgniter

  7. 7

    How can I run a function when a radio is selected and I click a button?

  8. 8

    Add class to selected radio button

  9. 9

    How to add checked class to a radio button in AngularJS

  10. 10

    bootstrap: change class of tr in a table when radio button selected

  11. 11

    How do I get the value of a user-selected radio button in the controller?

  12. 12

    how to retain radio button selected when i again view same view page?

  13. 13

    How to align label and radio button?

  14. 14

    How to add class to button of selected marker

  15. 15

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

  16. 16

    How can I run a function when a radio is selected and I click a button?

  17. 17

    add class for selected radio button in thymeleaf

  18. 18

    How do I change the name of a label if a specific radio button is checked?

  19. 19

    Add class to selected radio button

  20. 20

    How do I make different panes visible when button selected?

  21. 21

    Set the class on selected radio button

  22. 22

    Add class to the label for radio button in cakephp 3

  23. 23

    How to add and remove class on radio button?

  24. 24

    How do I get the correct radio button selected using MVC RadioButtonFor with float values?

  25. 25

    How to associate a label with the radio button?

  26. 26

    How to change button color when radio input is selected, but input is wrapped in div before label?

  27. 27

    How do I manage the layout in android after the Radio button is selected?

  28. 28

    How do i Get value of selected radio button in angularjs?

  29. 29

    How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)

HotTag

Archive