X

LATEST #JQUERY NEWS

jQuery’s .click() Method


With jQuery, it’s super easy to make something happen as a result of something else being clicked using the .click() method. All you need to do is pass a function through the .click() method. The .click() method will be the trigger event (make sure you use a selector to indicate which HTML element being clicked should trigger your code), and the code within the function that you’ll pass through the .click() method will execute the result of the click trigger. For example, maybe you want to remove a particular element once it’s clicked on. Your code would look like this:

$(".bye").click(function(){
$(this).remove();
})

By passing a function through the click method, you’re instructing the code to make sure you remove the $(“.bye”) element when it is clicked on. With jQuery, the possibilities are endless for the kind of action you want to occur after the .click() trigger is activated. You could toggle something, make it fade in or out, make it slide up or down, trigger an alert message…anything you can dream of, you can probably make it happen with a little (or a lot of) jQuery.

Let’s do one more example. In the code below, you’ll see that when the HTML element is clicked, another element will execute a .slideUp() animation:

$(“.click-me").click(function(){
$(“#menu”).slideUp();
})

Recently Published

article image
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of checkbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...