0

So my anomaly is this: I am passing an integer value 'g' to my 'turn' function inside my .addEventListener, for some reason, when the integer comes out on the other side through the document.getElementById("score").innerHTML=j line, it displays the sting 'length' when I fully expected the integer I passed in initially, why is this doing this?

  var initials=document.getElementsByClassName("letter")

  function load(){

    for(var g in initials)
    {
      initials[g].addEventListener("click", function(){turn(g)})
      initials[g].innerHTML=alpha[Math.floor(Math.random()*alpha.length)]
      document.getElementById("score").innerHTML="thischanged "+g
    }
  }

  function turn(j){
    document.getElementById("score").innerHTML=j
      }
Venkata Krishna
  • 1,768
  • 2
  • 14
  • 21
matt lao
  • 331
  • 1
  • 2
  • 11
  • Array iteration is one problem, see the duplicate to solve it. There's another problem though with treating `g` as if it were a number or string, however it's actually a DOMNode. Not sure what you're expecting from it, but maybe something like `g.innerHTML` or such. – deceze Feb 19 '15 at 06:08
  • @deceze I guess my real question is, how do I use this loop to assign a unique event listener to each node, based on the individual node itself – matt lao Feb 19 '15 at 06:34
  • @mattlao, I know it might not seem like it, but your question really is a duplicate. The problem you're seeing is because you're using `for/in` incorrectly. Read **3. Use for-in correctly** in the accepted answer in [the duplicate](http://stackoverflow.com/a/9329476/152786). – smathy Feb 19 '15 at 06:59

1 Answers1

0

construct for(var g in initials) enumerates property names. at least you need to pass in initials[g].

akonsu
  • 28,824
  • 33
  • 119
  • 194