The click()
method in jQuery is a built-in method that fires an event when the relevant element is clicked on. Clicked on simply means that the user presses and releases the mouse button on that element.
The click()
method has two ways that it can be called:
The illustration below shows the method signature:
Now that we know how to call this method, let’s look at both implementations of the click()
method
This implementation takes in input a function
and runs that when the click()
method is called.
body
of the HTML code and is written between the script
tag.h1
style.this
operator to apply the toggle
method on the object using a delay of 1000 ms.In this case, the cick()
method is called to fire an onclick
event already assigned to an element. This way a separate element can also be used to call the onclick
event of another element. The example below shows how this is done.
The first step is to first declare an object of a particular style attribute which is clickable and declare and onclick
action for it as shown on line 11.
The second step is to create a script
block in the body
of the code where the click()
method is now attributed to a different style format, p
.
The third step is to call the already defined onclick
method for h1
within this function call, as shown on line 15.
This then calls the same onlick
method for a particular attribute on a completely separate one.
Free Resources