jQuery HTML Element How to - Click to highlight anchor link








Question

We would like to know how to click to highlight anchor link.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.6.js'></script>
<style type='text/css'>
a {<!--   ww w .ja v  a 2 s. co m-->
  cursor: pointer;
}

.directionsselect {
  background-color: black;
  color: white;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    $(document).ready(function() {
        $('#outdirections a').click(function() {
            $('#outdirections a').removeClass('directionsselect');
            $(this).addClass('directionsselect');
        })
    });
});
</script>
</head>
<body>
  <div id="outdirections">
    <a>Item</a> <a>Item</a> <a>Item</a> <a>Item</a> <a>Item</a>
  </div>
</body>
</html>

The code above is rendered as follows: