javascript - jQuery: styling and unstyling on click radio button using parent(), next() -
i have 3 identical sections. exact same each other.
what i'm trying achieve when click on ".rad1"(first radio button), styles label.
when click on ".rad2", should unstylize first radio button , stylize second radio button.
want happen in own section, aka, when click on 1 section, styles applied section, no other section.
this have, haven't found way achieve it:
$('.rad1').click(function() { $(this).parent('.label1').css('background', 'red'); }); $('.rad2').click(function() { $(this).parent().next('.label1').css('background', 'transparent'); $(this).parent('.label2').css('background', 'red'); }); .form-section { background: orange; border: 1px solid black; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form-section"> <form action=""> <div class="first"> <label for="radio" class="label1"> <input type="radio" class="rad1" name="rad">first </label> </div> <div class="second"> <label for="radio" class="label2"> <input type="radio" class="rad2" name="rad">second </label> </div> </form> </div> <!------------section--> <div class="form-section"> <form action=""> <div class="first"> <label for="radio" class="label1"> <input type="radio" class="rad1" name="rad">first </label> </div> <div class="second"> <label for="radio" class="label2"> <input type="radio" class="rad2" name="rad">second </label> </div> </form> </div> <!------------section--> <div class="form-section"> <form action=""> <div class="first"> <label for="radio" class="label1"> <input type="radio" class="rad1" name="rad">first </label> </div> <div class="second"> <label for="radio" class="label2"> <input type="radio" class="rad2" name="rad">second </label> </div> </form> </div>
i'd this:
$('input[name="rad"]').click(function() { $(this).closest('form').find('label').css('background', 'transparent') $(this).parent().css('background', 'red'); }); $('input[name="rad"]').click(function() { $(this).closest('form').find('label').css('background', 'transparent') $(this).parent().css('background', 'red'); }); .form-section { background: orange; border: 1px solid black; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form-section"> <form action=""> <div class="first"> <label for="radio" class="label1"> <input type="radio" class="rad1" name="rad">first </label> </div> <div class="second"> <label for="radio" class="label2"> <input type="radio" class="rad2" name="rad">second </label> </div> </form> </div> <!------------section--> <div class="form-section"> <form action=""> <div class="first"> <label for="radio" class="label1"> <input type="radio" class="rad1" name="rad">first </label> </div> <div class="second"> <label for="radio" class="label2"> <input type="radio" class="rad2" name="rad">second </label> </div> </form> </div> <!------------section--> <div class="form-section"> <form action=""> <div class="first"> <label for="radio" class="label1"> <input type="radio" class="rad1" name="rad">first </label> </div> <div class="second"> <label for="radio" class="label2"> <input type="radio" class="rad2" name="rad">second </label> </div> </form> </div> it's better add , remove class instead of styling lables using .css().
Comments
Post a Comment