Example 6: Highlighting focus with JQuery

Best Practice Rating: Good

Example Start

Select pizza crust

Example End

HTML Source Code

<div class="radiogroup">
  <div class="label">Select pizza crust</div>
  <ul class="radio">
    <li><input type="radio" name="crust" id="crust1" value="deep" /><label for="crust1">Deep dish</label></li>
    <li><input type="radio" name="crust" id="crust2" value="thick" /><label for="crust2">Thick</label></li>
    <li><input type="radio" name="crust" id="crust3" value="hand" /><label for="crust3">Hand thrown</label></li>
    <li><input type="radio" name="crust" id="crust4" value="thin" /><label for="crust4">Thin</label></li>

Javascript Source Code

  <script type="text/javascript">
// Using JQuery selectors to add onFocus and onBlur event handlers

$(document).ready( function() {

  // Add the "focus" value to class attribute
  $('ul.radio li').focusin( function() {

  // Remove the "focus" value to class attribute
  $('ul.radio li').focusout( function() {



CSS Source Code

  <style type="text/css">
div.radiogroup div.label {
  margin: 0;
  padding: 0;
  margin-left: 20px;
  font-weight: bold;

ul.radio  {
  margin: 0;
  padding: 0;
  margin-left: 20px;
  list-style: none;

ul.radio li {
  border: 1px transparent solid;

ul.radio li:hover,
ul.radio li.focus  {
  background-color: lightyellow;
  border: 1px gray solid;
  width: 10em;