Let’s see a small comparison between a javascript code and JQuery.
Task: To show and hide a div tag with contents in it.
Most web developers come across this situation to show and hide the div contents – Let’s simply say it as toggle div tags
Now, comes the javascript code to toggle div tag
HTML
Javascript
<script type="text/javascript">// <![CDATA[
function toggleDivTag(id) { var divID = document.getElementById(id); if(divID.style.display == 'block') divID.style.display = 'none'; else divID.style.display = 'block'; }
// ]]></script>
function toggleDivTag(id) { var divID = document.getElementById(id); if(divID.style.display == 'block') divID.style.display = 'none'; else divID.style.display = 'block'; }
// ]]></script>
When you click on the link, the javascript function toggleDivTag was called along with a parameter, here ‘testfooter’. This parameter is the id of the div tag,. This div element should be closed and opened, when clicked.
In the javascript code, we are assigning the id of the particular div tag to a variable divID. Now, we are checking whether the div tag is visible or closed. If the div is shown, then we are applying the style as “none” to hide the div tag and vice versa.
Now, comes our hero JQuery.
JQuery can be downloaded from http://docs.jquery.com/Downloading_jQuery#Download_jQuery
HTML
<a id="clickhere" href="#">Click here</a>
<div id="hide_this">Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents</div>
<div id="hide_this">Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents Test contents</div>
<script src="jquery-latest.js"></script> <script type="text/javascript">// <![CDATA[
$(document).ready(function()
{
//$('#hide_this').hide();
$("#clickhere").click(function()
{
$('#hide_this').toggle('slow');
});
});
// ]]></script>
$(document).ready(function()
{
//$('#hide_this').hide();
$("#clickhere").click(function()
{
$('#hide_this').toggle('slow');
});
});
// ]]></script>
On the first line, you are including the jQuery library.
<script src="jquery-latest.js"></script>
$(document).ready(function()
$("#clickhere").click(function()
{
$(‘#hide_this’).toggle(’slow’);
});
{
$(‘#hide_this’).toggle(’slow’);
});
//$(‘#hide_this’).hide();
No comments:
Post a Comment