Hi,
I'm trying to learn jquery, I've some basics of javascript.
I've written this piece of code to toggle between 3 functions, it works fine
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
</head>
<style type="text/css">
.format1{
background: red;
}
.format2{
background: yellow;
}
.format3{
background: blue;
}
</style>
<div id="div-toggle">Click here to toggle</div>
<script language="javascript">
var click = 0;
$('#div-toggle').toggle(
function(){
var el = $('#div-toggle');
el.addClass('format1');
el.html('No. of clicks: ' + click++);
},
function(){
var el = $('#div-toggle');
el.removeClass('format1');
el.addClass('format2');
el.html('No. of clicks: ' + click++);
},
function(){
var el = $('#div-toggle');
el.removeClass('format2');
el.addClass('format3');
el.html('No. of clicks: ' + click++);
}
);
</script>
</html>
Here if you see the contents of all the functions in the toggle is similar. How can I improve this by moving these code to use a single function?