2

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() {}
asked Mar 1, 2018 at 9:52
2
  • 1
    First, your question is unclear. Second, If you have referred any post, it would be better to link them Commented Mar 1, 2018 at 9:54
  • Please formulate you're question correctly it's unclear what you're asking here ! Commented Mar 1, 2018 at 9:55

2 Answers 2

3

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>

answered Mar 1, 2018 at 10:02
Sign up to request clarification or add additional context in comments.

2 Comments

So, that solution would mean I keep my current thing and copy my functions' content within the part where you call the 'Called'-alert?
Yes. This wont be the best approach. But it will work.
2

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.

answered Mar 1, 2018 at 10:00

2 Comments

Could you maybe guide me a bit more within one of these solutions?
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; });

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.