Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript onclick stops working, multiple dynamically created divs
    primarykey
    data
    text
    <p>I have run into a strange problem, i am creating a lot of dynamically divs. And i recently found out that some of my divs doesn't fire the onclick event? All the divs are created using the same template, so why are some not working? Most of the time, its the 4-5 from the bottom. If you click on one of the others and then try again, you might get one of those to trigger. But only sporadically.</p> <p>Code to create the divs:</p> <pre><code>// Code to loop thru the number of players and create the divs accordingly for (fieldNumber = 0; fieldNumber &lt; 18; fieldNumber++) { var holderDiv = CreateDiv('gameCellLarge', ''); holderDiv.style.width = 150 + extraCellWidth + 'px'; // Ändra storleken beroende på antalet spelare if (fieldNumber == 0 || fieldNumber == 6 || fieldNumber == 8 || fieldNumber == 17) newField = CreateDiv('gameCellMedium borderFull gameText', gameText[fieldNumber]); else newField = CreateDiv('gameCellMedium borderWithoutTop gameText', gameText[fieldNumber]); holderDiv.appendChild(newField); for (playerNumber = 0; playerNumber &lt; players.length; playerNumber++) { holderDiv.appendChild(players[playerNumber].InitField(fieldNumber)); } gameFieldDiv.appendChild(holderDiv); } GameField.prototype.InitField = function(fieldNumber) { var newField = document.createElement("div"); if (fieldNumber == 0 || fieldNumber == 6 || fieldNumber == 8 || fieldNumber == 17) newField.className = 'gameCellSmall borderFull gameText gameTextAlign'; else newField.className = 'gameCellSmall borderWithoutTop gameText gameTextAlign'; var instance = this; if (fieldNumber == 6 || fieldNumber == 7 || fieldNumber == 17) { } else newField.onclick = function() { instance.DivClick(fieldNumber); return false; } this.fields[fieldNumber] = newField; this.score[fieldNumber] = 0; return newField; } </code></pre> <p>I added the return false to the click function, but it still behaves strangely. Why are some not triggering? I create around 18 divs / player. But it happens even if i just create one player.</p> <p>Do i perhaps need to cancel the event once i am done with it? (Like the return false; is trying to do) Works sometimes but not always...</p> <p>I have run out of ideas, here is a link to the script. Maybe i have just missed something. <a href="http://homeweb.mah.se/~M09K0291/789456/Yatzy/" rel="nofollow noreferrer">The script</a> I have changed it a bit, so just press the button and click on the lower end of the game field (yatzy, kåk etc). It should popup alerts when clicked. Sometimes they work sometimes they don't.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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