image - jquery, cannot add img in td -


the setup of html have alphabet imgs inside td. user should click imgs alphabetically, every correct click img removed. if wrong, xmark appear.

(not yet in code: after 3 wrongs, it's game over, , ask user if wants play again or go home)

this jquery code:

  var error = 0;         var check = 0;         $("table img").bind('click',function(){                 var letterarray = new array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');             var clickedvalue = $(this).attr('name');               if(clickedvalue ==letterarray[check]){                 $(this).parent().empty();                  check+=1;             } else {                 error+=1;                 $("error").add('<td><img src="images/xmark.png" alt="xmark" name="xmark"/></td>');                 if(error==3){                     alert("game over. homer eat you!");                     $("table img").unbind("click");                     $("table img").css("opacity", "0.4");                 }             }         });  

my table in html: posted first 6 letters brevity.

       <table>             <tr class="rows">                 <td><img src="images/z.png" alt="z" name="z"/></td>                 <td><img src="images/r.png" alt="r" name="r"/></td>                 <td><img src="images/s.png" alt="s" name="s"/></td>                 <td><img src="images/u.png" alt="u" name="u"/></td>                 <td><img src="images/b.png" alt="b" name="b"/></td>                 <td><img src="images/a.png" alt="a" name="a"/></td>             </tr>        </table>         <table><tr id="error"></tr></table> 

use .appendto() instead of .add()

try this:

var error = 0;         var check = 0;         $("table img").bind('click',function(){                 var letterarray = new array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');             var clickedvalue = $(this).attr('name');             if(clickedvalue ==letterarray[check]){                 $(this).parent().empty();                  check+=1;             } else {                 error+=1;                 $('<td><img src="images/xmark.png" alt="xmark" name="xmark"/></td>').appendto("tr#error");                 if(error==3){                     alert("game over. homer eat you!");                     $("table img").unbind("click");                     $("table img").css("opacity", "0.4");                 }             }         });  

demo


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -