Search code examples
javascripthtmlfor-loopgetattribute

javascript getAttribute stops the for loop


I'm doing a hangman game using only JavaScript HTML and CSS. In one of my functions I try to take a span's attribute named: value_. When the program reach that point, it runs once only. When I click on another letter it's not doing anything. If I put the getAttribute line in comments, the function run anytime I click on a letter. Any ideas? :\

That's the code:

//Search and Update function after click
function search_(target, letter)
{
    for (var i = 0; i < randomWord.length; i++)
    {
        //Identify the <span>'s id by the letter
        var target_ = document.getElementById(letter + i);
        //Get the <span>'s value_
        var attr_ = target_.getAttribute('value_');
        alert(attr_);
        /*if (randomWord[i] == attr_)
        {   
          target_.className = 'hide';
        };*/
    };
};

Solution

  • Alright guys, I have changed a bit the function. Now it's working correctly. Thank you for your help!

    function search_(letter)
      {
        for (var i = 0; i < randomWord.length; i++)
        {
          //Identify the guessWord <span>
          var targetNew = document.getElementById(i + '' + (randomWord.length-i));
          //Make it appear if the word contain the clicked letter
          if (targetNew.getAttribute('data-value') == letter)
          {
            targetNew.className = 'hide';
          };
        };
      };