Reputation: 31
I am new to the javascript. I want to double click a button and write something to the console but the code I use doen't work.
The code here: btn.addEventListener("dblclick",function);
This code didn't work at the Microsoft Edge. I looked the documentation. At the documentation there are two way.
First way is (this way is same with my way):
addEventListener("dblclick", (event) => {});
Second way is:
ondblclick = (event) => {};
I tried two way but it didn't work. And than I changed 'dblclick' to 'ondblclick' but it didn't work again. But at this times I didn't take any error or warning. At the last time I tried at the Google Chrome it worked successfully.
Why the same code worked at the Chrome but didn't work at the Edge? What can I do to solve this problem?
Upvotes: -1
Views: 57
Reputation: 21
If you have a code like you mentioned, btn.addEventListener("dblclick", function);
, then "function" is a reserved word, so you can't name your function that way. But since your code ran, I assume it was just an example.
So, I suggest you check whether you are actually getting the button to which you want to attach the event listener.
HTML
<button id="btn">Hello button</button>
JS
const btn = document.getElementById("btn");
console.log(btn); // Check if the element is retrieved
btn.addEventListener("dblclick", (event) => console.log("Hello world!"));
If you're not getting the element, it might be because your script is running before the HTML is fully loaded. This is a common mistake for beginners. The <script src="file.js">
tag should be placed at the end of the <body>
element.
<body>
<button id="btn">Hello button</button>
<script src="file.js"></script>
</body>
You can check which features are not supported in different browsers here: Can I use.
However, in your case, double-click events are supported in both Edge and Chrome.
Upvotes: 1