Skip to content

Instantly share code, notes, and snippets.

@willhalling
Created July 19, 2013 09:37
Show Gist options
  • Save willhalling/6037962 to your computer and use it in GitHub Desktop.
Save willhalling/6037962 to your computer and use it in GitHub Desktop.
Simple jQuery function to change class of labels when radio buttons are selected.
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<title>Select/Change option</title>
<style type="text/css">
.red {color: red;}
.black {color: black;}
</style>
<script type="text/javascript">
$(function() {
// When the value of the radio change
$('.radioPanel [type="radio"]').on('change', function() {
$(this)
.prev().addClass('red')
.siblings().removeClass('red');
});
});
</script>
</head>
<body>
<div class="radioPanel">
<label for="Milk">Milk</label>
<input type="radio" id="Milk" name="group1" value="Milk">
<label for="Butter">Butter</label>
<input type="radio" name="group1" value="Butter" checked>
<label for="Cheese">Cheese</label>
<input type="radio" id="Cheese" name="group1" value="Cheese">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment