Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-06-2012, 04:28 PM   PM User | #1
Styles2304
New Coder

 
Join Date: Jul 2005
Posts: 84
Thanks: 4
Thanked 0 Times in 0 Posts
Styles2304 is an unknown quantity at this point
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);
        }); 
Styles2304 is offline   Reply With Quote
Old 12-06-2012, 04:37 PM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,599
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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.
__________________
Don’t click this link!
VIPStephan is online now   Reply With Quote
Old 12-06-2012, 04:50 PM   PM User | #3
Styles2304
New Coder

 
Join Date: Jul 2005
Posts: 84
Thanks: 4
Thanked 0 Times in 0 Posts
Styles2304 is an unknown quantity at this point
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.
Styles2304 is offline   Reply With Quote
Old 12-06-2012, 05:27 PM   PM User | #4
Styles2304
New Coder

 
Join Date: Jul 2005
Posts: 84
Thanks: 4
Thanked 0 Times in 0 Posts
Styles2304 is an unknown quantity at this point
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!
Styles2304 is offline   Reply With Quote
Old 12-06-2012, 06:13 PM   PM User | #5
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,599
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
D’oh!
__________________
Don’t click this link!
VIPStephan is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:36 PM.


Advertisement
Log in to turn off these ads.