user2286580
user2286580

Reputation:

toggle classes for a child element

$('.toggle').addClass('change_button').removeClass('toggle');
$(this).removeClass('change_button').addClass('toggle');


i have provided the demo of complete code below in the first comment.
i have 2 sets of buttons

1st set
1.Lite
2.Medium
3.Heavy

Demo: Fiddle


2nd set
1.Yes
2.No

what i want is those sets must act as radio buttons like demo provided below in the first comment
instead of radio buttons i have here block buttons and when the button is selected i want to change bagraund color

Upvotes: 0

Views: 203

Answers (1)

PSL
PSL

Reputation: 123739

Try this:-

Demo

  $('.change_button.toggle',$(this).closest('div')).removeClass('toggle');
  $(this).addClass('toggle');

 });

Explanation:-

$('.change_button.toggle',$(this).closest('div')).removeClass('toggle'); 

This statement selects the elements with .change_button and .toggle class which resides in the parent div of the clicked element and removes its toggle class.

$(this).addClass('toggle'); 

Adds the class the selected element.

Upvotes: 2

Related Questions