Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing functions from within a Javascript Object (Javascript)
    text
    copied!<p>I am attempting to create a basic Hangman program. It's an array of tags which are assigned into an array of Objects (called Buttons). Each image is an image of a letter, so for example you would press the 'L' button, it would check whether this was in an WordArray (which is an array of chars), then it would act accordingly (to hide the letter, then update the gallows image if it needed to).</p> <p>I cannot get the onclick method of the images to access the checkinarray function. What am i doing wrong?</p> <pre><code>var LetterAmount = 3; //make this 26. checked. var WordArray = new Array(); var Buttons = new Array(); function checkinarray(num){ var z = 0; while (z&lt;4){ document.write("looping through word now"); if (num == WordArray[z]){ document.write("&lt;br/&gt;it is in the word"); } z++; } } function ButtonClicked(){ this.Image.src = "images/blank.jpg"; checkinarray(this.Number); } function Button(Image, Number) { this.Image = Image; this.Number = Number; this.ButtonClicked = ButtonClicked; } function initialiseletters(){ var x; //set up empty img tags for use for(i = 0; i &lt; LetterAmount; i++) { document.write("&lt;img id=" + i + "&gt;"); } for(x = 0; x &lt; LetterAmount; x++) { document.images[x].src = "images/" + x + ".jpg"; document.getElementById(x).onclick =function(){ Buttons[this.id].ButtonClicked(); } Buttons[x] = new Button(document.images[x], "" + x); } } function initialiseword(){ //WordArray = new Array(); WordArray[0] = 0; WordArray[1] = 1; WordArray[2] = 2; WordArray[3] = 3; } function initialise(){ initialiseword(); initialiseletters(); document.write("testing overall"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload