Why console.log() bypasses addEventListener? [duplicate] - javascript

This question already has answers here:
addEventListener calls the function without me even asking it to
(5 answers)
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 1 year ago.
I have a button which on click is supposed to execute the addEventListener function. On the callback function part I directly use a console.log.
<button id="btnTrans">Translate</button>
var buttonTranslate = document.querySelector("#btnTrans");
buttonTranslate.addEventListener("click", console.log("clicked"));
as soon as the DOM loads, "clicked" appears on the console. Shouldn't it wait for the event to happen first?

The console.log is immediately executed because it is not wrapped in a function. Wrap it in a function:
buttonTranslate.addEventListener("click", () => { console.log("clicked") });

Related

how to add a setTimeout on an onclick [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Calling functions with setTimeout()
(6 answers)
SetTimeout not delaying a function call [duplicate]
(7 answers)
Why is setTimeout executing immediately? [duplicate]
(4 answers)
Why is my function call that should be scheduled by setTimeout executed immediately? [duplicate]
(3 answers)
Closed 5 months ago.
I have an add to cart button. When I click on it two things happen at the same time. The class b-items__item__add-to-cart and the onclick.
I would like the onclick to be able to be executed 2 seconds after pressing the add to cart button
With my code it does not work
<a style="cursor: pointer; margin-bottom: 5px;" data-nom="2001" data-prix="1.10" data-qte="1" data-checkbox="2001" data-url="https://phil.pecheperle.be/image-perles/perle-verre-peche-gardon-2001.JPG" class="btn btn-primary ajouter-panier b-items__item__add-to-cart" onclick="setTimeout(ouvreMaJolieAlert(event), 2000);">add to cart</a>
You're attempting to give the output of ouvreMaJolieAlert(event) to setTimeout as the callback. The function executes, but the output value of that function is probably nothing, or something else (depends on your specific scenario), meaning that the delayed invocation fails. You've probably meant to create a wrapper over it:
setTimeout(() => ouvreMaJolieAlert(event), 2000)
However, personally, I'd recommend you to create a function that handles this from a script; or better yet, use the addEventListener method:
document.querySelector("#the-id-of-your-link-here").addEventListener("click", () => {
setTimeout(() => ouvreMaJolieAlert(event), 2000));
});
And if you're planning to create multiple links that will perform this action, you can just replace the ID with a class.

How can i correctly pass a function to a eventListener? [duplicate]

This question already has answers here:
Javascript, addEventListener callback function executes immediately and only once? [duplicate]
(3 answers)
Closed 1 year ago.
I made a basic eventListener which creates an alert on button click. This one doesn't work (it calls the alert on the page load)
function handleClick(){
alert("hi!");
}
document.querySelector("button").addEventListener("click",handleClick());
this one works instead, but i don't understand why
document.querySelector("button").addEventListener("click",function(){
alert("hello");
});
with handleClick() you are executing the function when that statement is read. You need to pass in the function handleClick reference.
This:
document.querySelector("button").addEventListener("click",handleClick);

EventListener calling a function() prematurely [duplicate]

This question already has answers here:
Javascript "addEventListener" Event Fires on Page Load [duplicate]
(2 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 5 years ago.
My EventListener is calling the function before the click with this code... what's wrong with that line?
It was working fine when I had the function inside the parameter, but now that i'm calling it from there instead it's calling it prematurely before any click occurs.
divs[i].addEventListener('click', xopush(i), null);
It's calling a function with and argument passing to the function()... not an alert.
//Instead of EventListener I usually prefer to use...
divs[i].onclick = function() {
xopush(i);
}
... which is a answer to this. However, I was looking forward to using the eventListener so I could turn it off after 1 click on the element.

How to pass argument to function without getting called javascript? [duplicate]

This question already has answers here:
Javascript "addEventListener" Event Fires on Page Load [duplicate]
(2 answers)
Closed 6 years ago.
var function1 = function (ans){
alert("A");
}
$(document).ready({
$("input[type='radio']").click(function1(ans));
});
If function1 has no parameter then we can just have .click(function1) and function1 will not be called.
Meanwhile, when there is a parameter, function1 is called immediately when document is loaded. How to pass a parameter to a function without having the function called ?
You have to pass a callback function:
$(document).ready({
$("input[type='radio']").click(function(){
function1(ans));
});
});
$("input[type='radio']").click(function1(ans)); triggers the click event handler automatically.

Javascript - `onload` function called before loading? [duplicate]

This question already has answers here:
Onclick event getting called automatically
(3 answers)
Closed 8 years ago.
I've created an iframe using javascript:
fr=document.createElement('iframe');
fr.src="www.example.com";
fr.onload=doSomething();
document.body.appendChild(fr);
problem is the onload part seems to be running before the iframe loads -
I get an error in the console (using chrome) about something undefined in the function (and it would be defined once the iframe loads), and when I check, it turns out the iframe was never even appended.
onload expects a function reference to call
fr.onload=doSomething;
When you assign doSomething() to the onload event handler, its return is called (which is undefined) and that is the error you see. This is also the reason why doSomething appears to run immediately (because it executes during the assignment).

Categories