Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2013
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    not working after score=3

    i just used jquery replaceWith but its seems not working after score=3. the fiddle shows what is going on. please click in this order like button1, button3, button2 then the score increase by point 1 for each button but after score=3 i.e., after clicking all three buttons the score stops and does not respond. i even tried with other button ids but its not working.below is the replace id that i assigned and as the text of the button changes but not responding to score and click.

    Edit fiddle - JSFiddle

    Code:
    $("#three").click(function () {
    $("#three").replaceWith("<button id=\"three\" type=\"button\" data-index=\"0\" >cat</button>");
    
    });

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,581
    Thanks
    23
    Thanked 644 Times in 643 Posts
    Not a solution but the reason why it stops working:
    This section:
    Code:
    var dataIndex = $(this).data('index');
    if (index == dataIndex) {
    	Score++;
    	if(dataIndex == 2) {
    		index = 0;
    	} else {
    		index++;
    	}
    }
    When the last button is clicked the dataIndex is 2. the
    Code:
    if(dataIndex == 2) {
    forces index to become 0. It never changes after that so the main IF loop never executes after the third button is clicked.
    Would this help do what you wanted?:
    Code:
    	var dataIndex = $(this).data('index');
    	Score++;
    	if(dataIndex == 2) {
    		index = 0;
    	} else {
    		index++;
    	}
        $("#score").html("Score: " + Score);
    });
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •