jQuery Page Widget How to - Toggle html content








Question

We would like to know how to toggle html content.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript'>
$(function(){<!--  ww  w .  j  av a 2s .c o  m-->
    $('#foo').toggle(function() {
        $(this).html('&#9650; HIDE &#9650;');
    }, function() {
        $(this).html('&#9660; SHOW &#9660;');
    });
});
</script>
</head>
<body>
  <div id="foo">&#9660; SHOW &#9660;</div>
</body>
</html>

The code above is rendered as follows: