I want to call a function which I call currently with each page-load ALSO with a click of the element #call-button. This is my current function:
window.addEventListener("load", function() {}
I hope a wrong solution clarifies a bit what I mean. So I am asking how to do that one here correctly:
window.addEventListener("load" || "#call-button.click" , function() {}
-
1First, your question is unclear. Second, If you have referred any post, it would be better to link themRajesh Dixit– Rajesh Dixit2018年03月01日 09:54:13 +00:00Commented Mar 1, 2018 at 9:54
-
Please formulate you're question correctly it's unclear what you're asking here !Bourbia Brahim– Bourbia Brahim2018年03月01日 09:55:18 +00:00Commented Mar 1, 2018 at 9:55
2 Answers 2
One way of doing it is bind the function to click of the element. And on window load trigger the element's click
document.getElementById('call-button').addEventListener('click', function() {
alert('Called!!!');
});
function onLoad(){
document.getElementById('call-button').click();
}
<body onload="onLoad()">
<button id="call-button">Click Me</button>
</body>
This function isn't defined, it is bounded to the event.
You can either do a click listener on your element and copy the function part, or you can create the function and both your listeners will call the function.
2 Comments
document.getElementById('call-button').addEventListener('click', function() { var randomIndex = Math.ceil((Math.random() * 10) % randomStrings.length - 1); newText1 = randomStrings[randomIndex]; newText2 = randomStrings2[randomIndex]; $(function gruesse_type() { $("#random_mantra").typed({ strings: [newText1], typeSpeed: 0 }); }); answer2.innerHTML = newText2; });