Class Match Radio Button

Published on

This is just a very simple code snippet that I often find myself writing. It uses jQuery, when a radio button is selected it adds a class to its label and removes the class from the other labels that are within the group (name). I tend to use classes as selectors, but this version is more generic.

    $('input[type="radio"]').change(changeTab);

    function changeTab() {
     var e = $(this)[0];
     $.each($('[name="' + e.name + '"]'), function(i,e) {
     $('[for="' + e.id + '"]').removeClass('active');
     });
     $('label[for="' + e.id + '"]').addClass('active');
    }