Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    May 2009
    Location
    China
    Posts
    133
    Thanks
    1
    Thanked 1 Time in 1 Post

    AddEvent Listener not working

    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:

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

    Code:
     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:
    Code:
    	// 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);
    	     }

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    I'm not familiar with the usage , but would this work?

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •