Posted by
Fahad Ibnay Heylaal
on Sep 10th 2008, 10:29
- in
JavaScript
What is Toggling?
Basically a show/hide feature for your webpage elements. For example, you may have a login box in your webpage, and you want your visitors to click on a button for showing or hiding the element.
This tutorial will show you how to implement a simple toggle effect in your webpage using jQuery.
As always, we will require some HTML content first
<a id="clickMe">Toggle my text</a>
<br />
<div id="textBox">This text will be toggled</div>
jQuery code
This is the code that makes the 'textBox' element toggle
But we want to toggle the element only when you click on 'Toggle my text' link. And this code will make it happen:
$(document).ready(function() {
$("#clickMe").click(function() {
$("#textBox").toggle();
});
});
Final Code:
<html>
<head>
<title>jQuery test page</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#clickMe").click(function() {
$("#textBox").toggle();
});
});
</script>
</head>
<body>
<a id="clickMe">Toggle my text</a>
<br />
<div id="textBox">This text will be toggled</div>
</body>
</html>