I have created a hangman based on a jQuery code that I found online. I understand how the code works and it works perfectly if I want to use only one word. I want to use game with multiple words, so I have created a function, in order to do so.
unfortunately, it doesn’t work with the function.
This is the function that I have created:
function hangman(var word)
{
var alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$.each(alpha.split(''), function(i, val){
$('#alpha').append($('<span class="guess">' + val + '</span>'));
});
$.each(word.split(''), function(i, val){
$('#word').append($('<span class="letter" letter="' + val +'">_</span>'));
});
$('.guess').click(function(){
var count = $('#word [letter='+$(this).text()+']').each(function(){ $(this).text($(this).attr('letter'));}).length;
$(this).removeClass('guess').css('color' , (count > 0 ? 'green' : 'red')).unbind('click');
});
}
$(document).ready(function() {
$('#BUT').click(function() {
hangman('DOG');
});
});
and this is my HTML:
<div id="hangman-jquery">
<div id="word"></div>
<div id="alpha"></div>
</div>
<button id= "BUT" ></button>
as I said, it doesn’t work. Do you know the solution by any chance?
seems to work then in some way: http://jsfiddle.net/BdkTC/