4

I've seen that you can attach events like this

<button type="button" id="myButton" onclick="myFunction()">

can you do the same without the "onclick=", like:

document.getElementById('myButton'). //and here attach the event on click to myFunction

I'm trying to keep JavaScript and HTML separate.

Joeytje50
  • 18,636
  • 15
  • 63
  • 95
user983124
  • 257
  • 3
  • 5
  • 8

6 Answers6

8

It's similar to the onclick approach, and in fact uses the same event-handler, but is removed from the HTML:

document.getElementById('myButton').onclick = function(){
    // do stuff
    myFunction();
}

If you don't have an id on the element you could also use:

var inputs = document.getElementsByTagName('input');

for (var i=0, len=inputs.length; i<len; i++){
    if (inputs[i].type == 'text'){
        // assuming you want to affect text-inputs in this case
        inputs[i].onclick = function(){
            // do stuff. In here 'this' refers to inputs[i] element
            myFunction();
        };
    }
}

An alternative approach, using Array.prototype.forEach(), with an array of elements created using Array.prototype.slice() and document.querySelectorAll():

[].forEach.call(document.querySelector('input[type="text"]', yourFunctionName);

This will execute the yourFunctionName() function for each <input /> element, of type="text", returned by document.querySelectorAll() passing that <input /> element into the function as this.

You could also use addEventListener() in this case:

document.getElementById('myButton').addEventListener('click', myFunction, false);

And also in this situation, using document.querySelector() (as opposed to document.querySelectorAll()), which returns the first element that matches the passed-in selector, using CSS notation:

// gets the element with an 'id' of 'myButton', binding the 'click' event-handler:
document.querySelector('#myButton').addEventListener('click', myFunction, false);

Or:

// gets the first of the <input> elements, binding the 'click' event-handler:
document.querySelector('input').addEventListener('click', myFunction, false);

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
4

Yes, you can (and should!).

document.getElementById('myButton').onclick = myFunction;
Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
4

Sure, you only need to select your item and call it's correspondant callback function. p.e:

document.getElementById('myButton').onclick = function(e) {
    // your code here
}

Or, without the inline function:

document.getElementById('myButton').onclick = myObject.myMethod;

https://developer.mozilla.org/en/DOM/element.onclick

elboletaire
  • 5,180
  • 2
  • 36
  • 46
  • 2
    +1 for avoiding the ever-prevalent inline function — they really are not the panacea so often claimed of them. – Lee Goddard Sep 20 '14 at 12:52
3

Something like this:

  document.getElementById('myButton').onclick = function() {location.href='http://stackoverflow.com';return false;}
Oyeme
  • 11,088
  • 4
  • 42
  • 65
2
document.getElementById('myButton').onclick = function () {
        console.log('Inline event attach');
    };

    document.getElementById('myButton').addEventListener('click', function () {
        console.log('Using native addEventListener');
    }, false);
Jags
  • 65
  • 1
  • 8
0
document.getElementById('myButton').onclick = function() { myFunction(); }
squarephoenix
  • 1,003
  • 7
  • 7