Search code examples
javascriptjqueryradio-buttontoggleclass

toggle classes between two radio buttons in jquery


I know this sounds like an easy question but for some reason I just can't figure it out right now. My question is this, let's say I have two radio buttons on my page:

<input type="radio" checked="checked" name=sport value="soccer" />
<input type="radio" name=sport value="basketball" />

What I want to do is that if I click on a radio button, I want to add a class name to it, and if I click on the other radio button, I want to add the same class to that new clicked radio button and I want the class that I added to the previous one removed, and so on....

Any idea please?


Solution

  • Try this.

    $("input[name='sport']").click(function(){
       $("input[name='sport']").removeClass('className');   
       $(this).addClass('className');
    });
    

    Demo