Javascript + HTML: Button Not Calling on Function Onclick -


i have made button:

<input type="button" value="a" onclick="searchletter(this)"></input> 

when clicked, supposed call on function checks if letter in word , if is, add spaces array in corresponding spot:

    function searchletter(obj)     {         var letter = obj.value;         obj.disable;         (i = 0; <= word.length; i++){             if (word[i] == letter) {                 wordspaces[i] = letter;                 document.getelementbyid('spaces').innerhtml = wordspaces.join('');                 break;             }         }     } 

however, button not calling on , not sure why.

here jsfiddle (hangman)

function pickword() {     var  word = dictionary[math.floor(math.random() * dictionary.length)];      var wordspaces = [];      (var = word.length - 1; >= 0; i--)      wordspaces.push("_ ");      document.getelementbyid('spaces').innerhtml = wordspaces.join('');   } 

in code, word , wordspaces local variable function.

but in

 function searchletter(obj) {      var letter = obj.value;      (var = 0; <= word.length; i++) { 

you're trying refer word variable. that's why it's not entering loop

so must like:

 var word, wordspaces;  function pickword() {      word = dictionary[math.floor(math.random() * dictionary.length)];      wordspaces = [];      (var = word.length - 1; >= 0; i--)      wordspaces.push("_ ");      document.getelementbyid('spaces').innerhtml = wordspaces.join('');   }  function searchletter(obj) {      var letter = obj.value;      (var = 0; <= word.length; i++) {          if (word[i] == letter) {              wordspaces[i] = letter;              document.getelementbyid('spaces').innerhtml = wordspaces.join('');              break;          }      }  } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -