...

View Full Version : AddEvent Listener not working



KevinJohnson
08-23-2011, 05:45 PM
I have more than 30 addEvent listeners i need to add to my WebApp. But the Code to dynamically add them is not working, unless change:



AddItemEvtAry[x].addEventListener('click', function (e) {document.getElementById("text" + x).value ++}, false);


to



AddItemEvtAry[x].addEventListener('click', function (e) {document.getElementById("text0").value ++}, false);



but obviously hardcoding all of those DIV IDs would defeat the purpose of the code.


Code in Question:


// Event Listeners for Incrimenting Quantities
var AddItemEvtAry = [40];

for(var x = 0; x < 39; x++)
{
AddItemEvtAry[x] = document.getElementById("AddItem" + x);
}

for(var x = 0; x < 39; x++)
{
AddItemEvtAry[x].addEventListener('click', function (e) {document.getElementById("text0").value ++}, false);
}

DanInMa
08-23-2011, 07:35 PM
I'm not familiar with the usage , but would this work?




for(var x = 0; x < 39; x++)
{
document.getElementById("AddItem" + x).addEventListener('click', function (e) {document.getElementById("text"+x).value ++}, false);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum