jQuery HTML Element How to - Programatically change a link's state to :active








Question

We would like to know how to programatically change a link's state to :active.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<style type='text/css'>
a {<!--from  w  w w.j  a  v a 2  s . c  om-->
  color: blue;
}

a:active, a.active {
  color: red;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    $(document).ready(function () {
        $('button').click(function () { $('a').toggleClass('active') }); 
    });
});
</script>
</head>
<body>
  <a href="#">Link</a>
  <button>Activate/Deactivate</button>
</body>
</html>

The code above is rendered as follows: