3

I'm starting to learn JavaScript and I would know how to call a function from an event. The only way I know is with anonymous functions (see my code) and I don't know how to call it in a simpler way. Thanks in advance!

    function example (){
        document.getElementById("myDiv").onclick = function (){
            example2 ();
        }
    }

2 Answers 2

4

You can just assign it by name:

function example (){
    document.getElementById("myDiv").onclick = example2;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much kamituel, I was trying it always with the () after its name and didn't work. I'm a total beginner as you can see :)
You're welcome. By the way - if you found my solution correct, you can mark it as an accepted answer - this way other people who see this question in the future will know it worked.
1

Try

document.getElementById('myDiv').onclick=example2;

Comments

Your Answer

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.