Web Analytics Made Easy -
StatCounter AddEvent Listener not working - CodingForum

Announcement

Collapse
No announcement yet.

AddEvent Listener not working

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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);
    	     }
    - Firebug is a web developers best friend! - Learn it, Love it, use it!
    - Validate your code! - JQ/JS troubleshooting
    - Using jQuery with Other Libraries - Jslint for Jquery/other JS library users

    Comment

    Working...
    X