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 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    4
    Thanked 0 Times in 0 Posts

    append() Not working as anticipated

    I've programmed a simple dice roller but when I append the values to the div, they show up for a split second and then disappear. It only does it for the custom roll section where you input data (ex: 2d20+3d20) into a form and submit.

    Here's where I generate the values. I don't expect the problem to be here as you can see all the values for a split second unless maybe it's something in the way that diceUpdate() is called.
    (Note: This is only wrapped in PHP tags to make distinguishing the code simpler)
    PHP Code:
            //Custom Rolls
            
    $("#diceCustom").submit(function() {
                var 
    rollText = $("#diceCustomText").val();
                
    //Splits the string into separate dice rolls
                
    var rollSplit rollText.split(",");

                for (var 
    0rollSplit.lengtha++) {
                    
    //Splits rolls that require addition
                    
    var additionSplit rollSplit[a].split("+");
                    var 
    diceTotal 0;
                    for (var 
    0additionSplit.lengthe++) {
                        
    //Splits each individual roll into sides and number of dice
                        
    var diceSplit additionSplit[e].split("d");
                        
    diceTotal diceTotal diceRoll(diceSplit[0], diceSplit[1]);
                    }
                    
    diceUpdate(rollSplit[a], diceTotal);
                }
            }); 
    And here is the portion of code that actually updates the div to display the value:
    PHP Code:
            function diceUpdate(dicetotal) {
                var 
    rollUpdate '<span style="color: #000;"><b>' total '</b> : ' dice '</span><br>';

                $(
    "#diceWindowDiceRolls").append(rollUpdate);
                $(
    "#diceWindowDiceRolls").animate({scrollTop: $("#diceWindowDiceRolls").prop("scrollHeight")}, 10);
            } 
    The really interesting thing is that I have single quick rolls setup and they work properly:
    PHP Code:
            //Quick Rolls
            
    $("div.dice").click(function() {
                var 
    sides = $(this).attr("value");
                var 
    roll diceRoll(1sides);

                if (
    roll == sides) {
                    var 
    rollColor '0F0';
                } else if (
    roll == '1') {
                    var 
    rollColor 'F00';
                } else {
                    var 
    rollColor '000';
                }

                var 
    rollUpdate '<span style="color: #' rollColor ';"><b>' roll '</b> : 1d' sides '</span><br>';
                
                $(
    "#diceWindowDiceRolls").append(rollUpdate);
                $(
    "#diceWindowDiceRolls").animate({scrollTop: $("#diceWindowDiceRolls").prop("scrollHeight")}, 10);
            }); 

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,695
    Thanks
    6
    Thanked 1,011 Times in 984 Posts
    I would imagine that the problem is the loop. You are executing diceUpdate() with all its contents (animation etc.) as many times as rollSplit is long. It’s hard for me to imagine without seeing and playing around myself, though.

  • #3
    New Coder
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I'll upload a version shortly. The reasoning for that is that you can enter complex dice rolls with addition and multiple separate rolls. Ex: 2d20+1d12,2d4,3d6. In that case it would run diceUpdate() 3 times to display 3 separate values.

  • #4
    New Coder
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I was just playing around with it some more . . . the issue was that it was refreshing the page . . . I forgot to include event.preventDefault() so the form was actually submitting!

  • #5
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,695
    Thanks
    6
    Thanked 1,011 Times in 984 Posts
    D’oh!


  •  

    Posting Permissions

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