1

Previously, I had this line in my HTML file:

<input type="button" value="1" name="btn1" id="btn1" onclick="showNum(1)" />

This works fine, but I don't want to include the method name in the HTML file. I would like to have something like this in the HTML:

<input type="button" value="1" name="btn1" id="btn1" />

And then I want to bind this button to the method in the JS file. I tried this code and it doesn't work:

window.onload = function() {
    document.getElementById("btn1").onclick = showNum(1);
};

How can I bind btn1 to run showNum(1) using JavaScript?

3
  • Is the argument to showNum always the same as the value of the input? Commented Sep 19, 2019 at 19:07
  • Yes. Do you think there's a better way? Commented Sep 19, 2019 at 19:09
  • Yes, see this answer Commented Sep 19, 2019 at 19:20

5 Answers 5

2

It is also possible to bind a reference to the function, like so:

var showNum = function(){
    alert(this.value);
}
window.onload = function() {
    document.getElementById("btn1").onclick = showNum;
};
<input type="button" value="1" id="btn1" />

Sign up to request clarification or add additional context in comments.

Comments

1

I can't tell you exactly why it works like this, but if you use an anonymous function with showNum(1); inside of it when assigning to onclick, it works fine.

window.onload = function() {
    document.getElementById("btn1").onclick = function () {
        showNum(1);
    }
};

Also a tip, window.onload can only be used once, so if you have a master page in your site that also needs to run a script when the page starts, it can be overridden. Use a self-starting function like this and then you won't have to worry about that, and you can have as many as you want. Plus it's cleaner and looks nicer (at least to me).

(function () {
    document.getElementById("btn1").onclick = function () {
        showNum(1);
    }
})();

Just make sure it's after the rest of your JS code and HTML, since pages load from the top down.

4 Comments

I tried using the self-starting function and it doesn't work. The first part, = function(){ showNum(1); } works, though.
Is it at the bottom of your page, after all the HTML? Pages load vertically, and the downside of the self-starting function is that the element has to exist before you can call it. Personally I have no issue with that, in general it's good practice to put all scripts at the bottom of your page.
Oh, it has to be below the HTML too? I had it at the bottom of my JS at the top of my HTML. That's kind of a bummer and would require me to move my JS out of <head>.
Is there a need to keep it in <head>? It's a common convention to put JS code at the bottom of the page (such as in a footer, or just floating after the body), mostly because the user will see a page load faster. If you make the user wait for the DOM to get through all the JS in the header, your page will load slower. Personally I think it works better for organization too, but at the very least, you should try to keep JS after the HTML on a page.
1

You need to create a new function that calls showNum() with the argument you want.

window.onload = function() {
    document.getElementById("btn1").onclick = function() {showNum(1);};
};

You can also use the .bind() method to bind the argument.

window.onload = function() {
    document.getElementById("btn1").onclick = showNum.bind(null, 1);
};

2 Comments

What does the first argument of bind do? (the null)
@AaronFranke first argument is this, and because we don't care in this case where this points to we pass null instead
0
window.onload = function() {
    document.getElementById("btn1").addEventListener('click', ()=>showNum(1));
};

Comments

0

showNum instead of showNum(1)

showNum - is handler, but showNum(1) - is result of the execution.

if parameter is required: showNum.bind(this, 1);

Or even better - addEventListener can be used: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

2 Comments

But the method takes one argument, which I need to pass in.
@AaronFranke if parameter is required: showNum.bind(this, 1);

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.