Whenever an HTML element is clicked, there is the occurrence of the jQuery click event. To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”).click() method. The user can attach a function to a click method whenever an event of a click occurs to run the function. A piece of code is thereby executed whenever an event of click() is triggered.
The mouse click is simulated through the HTMLElement.click() method.
Click() function is an in-built function in jQuery that starts a click event. Whenever a relevant element is clicked upon, an event is fired by a click() method. This event then fires a click event of elements higher up in the document tree (or event chain).
As the name suggests it is simply the pressing and releasing of the mouse button by the user over the element.
One or more event handlers are attached to the selected elements through the on() method. On clicking on an element, an event of click() occurs. A click() method is executed once an event of click() occurs. A function is attached to a click() method and executed. A click() method is used with other events of jQuery.
- .click( handler ).
- .click( [eventData ], handler ).
The first and the second variations are a short form for .on( “click”, handler ) and the shortcut for the .trigger( “click” ) in the last variation.
Whenever a mouse pointer is over the element and the button pressed and released, the event click() is then sent to that element. The event can be received by any element of the HTML.
It illustrates the following
The event handler can bind to any <div> i.e. target or other.
With the above code, if the element is clicked, the following will be displayed.
Click() event can also be triggered if the other element is clicked through the following code.
With this code, the message will be alerted by the element
Through the following events, only a click() event is triggered.
- When the pointer of a mouse is inside an element and the button of the mouse is depressed.
- When the pointer of a mouse is inside an element and the button of the mouse is released.
Before taking an action, the above-mentioned events are usually the desired sequence. Else, the other suitable event is the mouse down or mouse up event.
Due to the .click() method being the shortcut for .on( “click”, handler ), the possibility of detaching is present through the use of .off( “click” ).
Two ways are present that can be used to call a click() method:
- With a given function argument
- Without a given argument
The onclick function in jQuery can be used in elements of HTML like div, paragraphs, hyperlinks, etc. for accomplishing the desired task. A click is a mouse event that occurs on the depression of the button on the mouse. However, the event will occur if the pointer in a mouse is within the element.
$(selector).click(); -> used for triggering a click event for the selected elements.
$(selector).click(function); -> used for attaching function to a click event.
- The optional parameter used is the “function” which is used for execution on the occurrence of a click event.
- Return value: The selected element is returned that is specified with the function to perform.
The syntax is represented below.
Implementation of the method click()
1. When a function argument is given
In this type of implementation, a function is taken as an input and is executed whenever the method of click()is called.
- The writing of the code is between the script tag and within the HTML body of the code.
- The calling of the function is done as an h1 style attribute.
- An argument is declared with the function and is taken as a function separately.
- The method toggle is applied over the object using this operator with 1000ms delay.
- An example of the implementation method with a given function argument is shown below.
The output of the code will be “Clicking me will toggle the text!”
2. When no argument is given
- An object has to be declared first with an attribute of a particular style that is clickable. An onclick action has to be declared for the object.
- A block of script is to be created in the code’s body where the attribution of the method click() is done to a format of different style, p.
- The h1 onclick method which is already defined is to be called within the function call.
- The same onclick method is then called upon for an attribute.
- An example of a code showing the implementation of a click() method without a given argument is shown below.
The output of the code is “Clicking me will call an alert!”.
- The code use is:
- Inside the head section, the library jQuery is included followed by the calling of the <div> element. Using the class name that is in the section script, the <div> element is called through the method of. Finally, the alert will be delivered through the event handler.
- As already mentioned above in the article, an event of click occurs only when a mouse pointer is within the <div> element and the mouse button is depressed or released. Therefore if the user clicks inside the <div> element and then drags the pointer outside the <div> element while still pressing the button and without the release of the button in the mouse, there will be no event of click.
- The shorthand of jQuery .on(“click”, event_handler) is $.click. The example shows the use of the former variation of the method. The alert thrown is shown below.
- The code used is
- Many events can be used in the $.on method like dblclick, change, and others.
- Rendering an HTML button and adding text to the page on clicking of the button. Click() method is most commonly attached to a button, however, it can be attached to any other elements in a web page.
- A basic button is rendered with an id of “btn-primary” which is inside a <div> with an id of “main.
- Through the code, new content is to be displayed with a class of “new content” in the <p> tag after the clicking of the button.
- The jQuery is enclosed within the <script> tags.
- The method click()is attached to the button’s id and an argument is taken as a call back function which is an anonymous function.
- The string “Button clicked” is returned by the callback function.
- A function without a name is simply termed as an anonymous function and used as callbacks as arguments of a method. For building any responsive websites and applications, the anonymous functions and the callback methods act as the cornerstone.
- The code will start with the button inside the <div> “main”.
- Once when the button is clicked, the callback function will be called by the event handler as click() method is attached to the button selector.
- The string “Button clicked” will be displayed.
- The above code will render the content only once, however for displaying the string every time the code is clicked, append() is used instead of HTML().
- It will produce the string three times as the new content inside the <p> tag.
- The callback function is called each time the button is clicked.
If you have an urge to learn more about web development and get specialized in other related areas, then you can check the course “Master of Science in Computer Science” offered by upGrad. Specially designed for entry-level professionals (both male and female within 21 to 45 years of age), the course will be on an online platform with live sessions, for the ease of long-distance commuters. With over 30+ projects and assignments, the student will be getting the benefits of IIIT-Bangalore and LJMU alumni status. Feel free to contact our team for any assistance.