Moving on, in today’s section, we’ll be looking at double click events which are similar to the single click events that we saw the last time. There isn’t a lot of difference between them… In double click events, we use the leftmost button of the mouse. If we look at the mouse here, there are two buttons, the right and the left. We usually use the left button for double clicks. So now we’ll look at how to bring a double click event to any place using the left button of the mouse with different methods.
So, let’s see this,
The first method we will look at is to enable a double click event on an element using HTML.
Now I’ll open Sublime Text and we’ll see the program for a double click event. So, first of all, set it to HTML.
Here we write, example…. double click(3 seconds pause ;typing) Script.
(3 seconds pause ;typing)
We’ll go to our folder…. Here we will save it as a…dblclick, add an underscore 1, here but we’re adding underscore html. Because we are going to perform a double click through HTML.
So, let’s say I have a button. Here I have a button, but in double click it is not mandatory to take a button, or you can take something else also. Usually when we work with UI, that is User Interface, when we work in UI, buttons are commonly used for single clicks. The user is used to the habit of using the button by clicking it only once. It is a button, so we can click only once.
The button normally doesn’t have a double click option. So, even though we can apply a double click on a button without a problem, it is avoided keeping the UI perspective and the user’s habit in mind. Therefore a button commonly has a single click option, so, currently even while taking the example of double click on a button to learn the process, keep in mind the UI perspective. Suppose you have a header for example h1 or h2 or a division, div where we need to identify a division and have it perform something on a double click, these are situations where you would usually apply the double click event.
Clear ? So now,
Let’s take an h2 tag here and write
Double click ….on this….. heading.
Let’s run what we’ve written. Here we have the program with the writing.
“Double click on this event”
Obviously nothing will happen on clicking it as we’ve not enabled it. Let’s say we give it some style in the following way.
(3 seconds pause ;typing)
Style (3 seconds pause ;typing) H2…. text align…(3 seconds pause ;typing)... center
Now we have it in the center. Let’s say when I double click on it, I want it to set on a line within a paragraph, like you’re starting to read from that line.
Such as we are learning this from learnvern.
I’ll take a “p” tag here…P ID equal to…we can take any idea such as p1, let’s say I am taking an id, “p1”.
Suppose you want the line to set on p1 when you double click. So, here we’ll add “on…d b l …click” equal to “onDoubleClick()”. Note that I’ve used an uppercase “D” here to differentiate it. We’ve now written our on double click event. We’ll now define what happens in our on double click event.
Let’s take a function…. on double click (5 seconds pause ;typing)
Document dot getelement by ID
And here P1…dot..innerHTML equal to..this is.. Learnvern
We’ll save and reload it now. You see nothing happens when I click it, but when I double click it, we can see the line that is set which says “This is LearnVern”.
So, you see it happens when I double click on it. For a proper view, you can write “h2 comma p” to place it… exactly below it. Here we have the double click where you can click it twice to view the sentence. It goes away when you reload it and on double click the text will be set again. Therefore, here you see we’ve applied our On double click event on the HTML element “h2”.
I hope everything is clear, so far.
First let’s give an ID to H2 so that it gets identified. ID H2, since H2 is also the name of tag, let’s write Header here…. Here we will write (5 seconds pause ;typing) document…dot..get element…(3 seconds pause ) by ID Header2 dot.., on double click
After the main portion of ‘on D B L on double click’ we will add the function.
We will give a space equal to the function that
we have already written, I’ll place it here. Here we have the function.
So the function name, and we will remove the space.
(5 seconds pause ;typing)
And this is our function.
Control plus S, Save it and you have your program.
If you double click on the heading, you will get the same output of “This is LearnVern”. The only thing we’ve changed from the previous method is that we’ve given h2 an id, we got h2 by get an element by ID and after getting it and adding on double click and the function without adding the name of the function, we have to directly write the event we want to fire.
Next on the slide we have the third method using the addEventListener. Like on double click, we have to define the addEventListener method and pass the function. Let’s say you copy the same thing to a new program. Write the following.
Example double click….(6 second pause ;typing) addeventlistener
Remove the on double click
ID…. header1 ….
In this case you need the function, so document…dot ..get element by Id …
Here; our header1 and after the main portion of add…..Event…Listener, put the listener which, please note is not on double click, it is d b l c l i c k, double click. And after the comma, add the name of your function. Save… and run the program.(3 seconds pause ) double click..
This is our program.
Here, as soon as I double click, I get the output, that is you can see LearnVern. As you can see, the output of all three programs is the same, there is no difference.
The only difference is in the implementation of the programs and how we’ve executed them.
You can see in the first program we’ve added on double click in the HTML portion. In the second program, we’ve added on double click on the object reference of our element and in the third program we’ve added the addEventListener. These are the three ways we’ve taken to implement the double click event. You can use them according to your feasibility and requirement, be it website development or web application development, wherever you need it, it can be used appropriately. All three give the same output.
You can decide which method you want to use.
So this was our topic of double click events.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
SENTHIL KUMAR BALU
superb in explanation and indentation a person missed this class if not known about the Learnvern
It is a very helpful course for me.
I am so happy to learn it and i learned lot of things
Superb and amazing 😍 🤩 enjoyable experience.
Bablesh kumar AAzad
exlent teching but problem is when we ask any doubt we have been getting very late response and some times we did not get the response.
Very Good Course
Honestly such a quality content with precise information have not found anywhere before this not even paid courses does not offer such content. That too free of cost. Really love it. Thank you so much