jQuery click() Method


jQuery click Method

With jQuery click Method 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.

The click() is an inbuilt method in jQuery that starts the click event or attach a function to run when a click event occurs. Syntax: $(selector).click(function); Parameter: It accepts an optional parameter “function” which is used to run when a click event occurs.

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.

$(“.click-me”).click(function() {

$(“#menu”).slideUp();

})


Leave a Reply

Your email address will not be published. Required fields are marked *