How do I debug JavaScript .onlick event?
Posted
by
user3700824
on Stack Overflow
See other posts from Stack Overflow
or by user3700824
Published on 2014-06-02T20:52:32Z
Indexed on
2014/06/02
21:27 UTC
Read the original article
Hit count: 227
JavaScript
I'm learning JavaScript but seem to be having a problem. I have a piece of code that is executed when the page is fully loaded. The function then gathers all the button elements on my HTML page. From here I loop through getting each button's title
attribute and then assign an onclick
event to the button that is equal to a function that writes to the console.log
with the title.
I have tried various ways of doing this but it is not working. Here is the JavaScript code that I'm working with. Currently all it does is loop through calling the function and logging the tile to the console.log
, but this is not supposed to happen. Each time I click the button it should call the function with its title and log that.
window.onload = myPageIsReady;
function myPageIsReady(){
var myList = document.getElementsByTagName("button");
var myTitle = [];
for(var i = 0; i < myList.length; i++){
myTitle[i] = myList[i].getAttribute("title");
myList[i].onclick = getMyTitle(myTitle[i]);
};
function getMyTitle(myTitle){
console.log(myTitle);
};
};
© Stack Overflow or respective owner