jQuery Tip – A Quick look at .click()
Here is a quick jQuery tutorial, or tip, focusing on the .click() event. We will create a simple example that lets the user click on a couple of options to manipulate a box of text below. Because this is just a quick tip on .click() I won't get into the other methods we are invoking on the click event. You can browse around the jQuery documentation to find out more – or better yet sign up for the rss feed here at freeyourdesign and come back for more jQuery tutorials. Anyway we will be changing the content boxes background color and making and slide up and down.
Don't forget to include the jQuery library
You'll need to include the jQuery library in the
of your html document in order for this tutorial to work correctly. The easiest way, and some say the best, is to grab the url for the jQuery files hosted by Google. You can use the call below:A little bit of CSS
Make it look a little less plain.
Build a Box
Next, we will throw together some HTML really quick to manipulate using jQuery. Nothing fancy here – a couple list items, a div and some paragraph content. (I'm using generic id and class names for this example – when coding you should try to use descriptive id and class names)
<li id="color"><a href="#">Change Color</a></li>
<li id="slide"><a href="#">Slide</a></li>
</ul>
<div id="box1">
<p id="text1">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam eu nulla. Donec lobortis purus vel urna. Nunc laoreet lacinia nunc. In volutpat sodales ipsum. Sed vestibulum. Integer in ante. Sed posuere ligula rhoncus erat. Fusce urna dui, sollicitudin ac, pulvinar quis, tincidunt et, risus. Quisque a nunc eget nibh interdum fringilla. Fusce dapibus odio in est. Nunc egestas mauris ac leo. Nullam orci.</p>
</div>
Finally some jQuery
Start off with a couple of script tags – just like JavaScript. In between the script tags the jQuery code starts with a document ready call.
jQuery Click and Change Background Color
We want the click event to happen on the list items we created – Change Color and Slide. First lets make the background color of the content box change.
We use jQuery to call the .click() event on the list item for Change Color which is <li id="#color"> – let's grab that element by it's ID name and add the .click() method to it:
});
The function() {} inside the .click() event is going to tell jQuery what we want to do once the ID is clicked. So, let's add some CSS to the ID and give it a background color:
$('#box1').css("background","#9f9");
});
Simple – try it out – click on Change Color and the content box background should change to a greenish color.
jQuery Click Toggle Slide Up and Slide Down
We are going to use the same .click() method on the other list item we created – <li id="slide">.
});
Now to make it slide up and then slide down using the jQuery slideToggle method on the box.
$("#box1").slideToggle("slow");
});
There you have it – a couple of quick and easy examples of the jQuery .click() method. See how easy jQuery just made it to create a cool little item that slides up to 'disappear' and the slide back down 'into view'.
See the Results
Here is all the code put together:
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#color').click(function() {
$('#box1').css("background","#9f9");
});
$('#slide').click(function() {
$("#box1").slideToggle("slow");
});
});
</script>
<style type="text/css">
a{color: #fff; text-decoration: none;}
#box1
{
width: 400px;
border: 1px solid #000;
padding: 10px;
}
#list1
{
margin: 0;
padding: 5px;
}
#list1 li
{
display: inline;
margin-right: 10px;
background: #2c2721;
padding: 5px;
}
<body>
<ul id="list1">
<li id="color"><a href="#">Change Color</a></li>
<li id="slide"><a href="#">Slide</a></li>
</ul>
<div id="box1">
<p id="text1">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam eu nulla. Donec lobortis purus vel urna. Nunc laoreet lacinia nunc. In volutpat sodales ipsum. Sed vestibulum. Integer in ante. Sed posuere ligula rhoncus erat. Fusce urna dui, sollicitudin ac, pulvinar quis, tincidunt et, risus. Quisque a nunc eget nibh interdum fringilla. Fusce dapibus odio in est. Nunc egestas mauris ac leo. Nullam orci.</p>
</div>
</body>
</html>
Popularity: 6% [?]