Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9266173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:15:26+00:00 2026-06-18T14:15:26+00:00

The title maybe is a little wrong but thats what I think the function

  • 0

The title maybe is a little wrong but thats what I think the function should to to change to the right class.

So this is my problem, i have created a dice roller, that count the sum of the dices.
The thing is that the counter is images switching after the sum of the dices, and they are called by class name from zero to nine.

When i have one dice with number 5, the image 5 shows, but when i have 2 dices with sum lets say 5 + 2 image 5 and 2 shows and not 7. so the classChange wont work.
I tried to insert a element.className = ” ” but it change the variable right away since i put it in a loop. well, hard to explain so I did a jsFiddle.

http://jsfiddle.net/3nZNM/3/

The code for the counter are row between 81 and 125

function numberTotext(nr){
    var arrClass=["zero","one","two","three","four","five","six","seven","eight","nine"];
    ret=[];
    nr=nr.toString();
    for(i in nr){
        ret.push(arrClass[nr[i]]);
    }
    console.log(ret);
    return ret;
}
numberTotext(total)

        function turnCounter(){       
        for (var i = 0; i < arguments.length; i++){ 
            dice_toolbar_counter_wrapper_Li = createElementWithClass('li',arguments[i]);
            console.log(dice_toolbar_counter_wrapper_Li)
            dice_toolbar_counter_wrapper_Ul.appendChild(dice_toolbar_counter_wrapper_Li);
            document.getElementById("ul");
        } 



    }   turnCounter(ret);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:15:27+00:00Added an answer on June 18, 2026 at 2:15 pm

    Remove element before appending new one when you calculate the total each time, so the counter only shows one time and not multiple times. Also you have to change CSS to make it easier to be able to calculate what class you have to use.

    WORKING JSFIDDLE DEMO

    Change relevant parts of CSS to:

    .dice-toolbar-counter-wrapper .side-0
    {
        background-position : 0 0;
    }
    
    .dice-toolbar-counter-wrapper .side-1
    {
        background-position : 0 -17px;
    }
    
    .dice-toolbar-counter-wrapper .side-2
    {
        background-position : 0 -34px;
    }
    
    .dice-toolbar-counter-wrapper .side-3
    {
        background-position : 0 -51px;
    }
    
    .dice-toolbar-counter-wrapper .side-4
    {
        background-position : 0 -68px;
    }
    
    .dice-toolbar-counter-wrapper .side-5
    {
        background-position : 0 -85px;
    }
    
    .dice-toolbar-counter-wrapper .side-6
    {
        background-position : 0 -102px;
    }
    
    .dice-toolbar-counter-wrapper .side-7
    {
        background-position : 0 -119px;
    }
    
    .dice-toolbar-counter-wrapper .side-8
    {
        background-position : 0 -136px;
    }
    
    .dice-toolbar-counter-wrapper .side-9
    {
        background-position : 0 -153px;
    }
    

    Change JS to:

    /*###CREATE ELEMENT WITH CLASS ###*/
    
    function createElementWithClass(elementName, className)
    {
        var el = document.createElement(elementName);
        el.className = className;
        return el;
    }
    
    /*###CREATE ELEMENT WITHOUT CLASS ###*/
    function createElementWithOutClass(elementName)
    {    var el = document.createElement(elementName);
        return el;
      }
    /*###CREATE ELEMENT WITH ID ###*/
    function createElementWithId(elementName, idName)
    {
        var element = document.createElement(elementName);
        element.id = idName;
        return element;
    }    
    var outerDiv = createElementWithClass('div', 'dice-window-wrapper'),
        innerDiv = createElementWithClass('div', 'dice-menubar-wrapper');
        outerDiv.appendChild(innerDiv);
        document.getElementById("page-content-wrapper").appendChild(outerDiv);
    
        dice_windows_wrapper_close = createElementWithClass('div', 'close');
        innerDiv.appendChild(dice_windows_wrapper_close);
        document.getElementById("dice-window-wrapper");
    
        dice_toolbar_wrapper_close = createElementWithClass('div', 'dice-toolbar-wrapper');
        outerDiv.appendChild(dice_toolbar_wrapper_close);
        document.getElementById("page-content-wrapper");
    
        add_remove_roll = createElementWithOutClass('ul');
        dice_toolbar_wrapper_close.appendChild(add_remove_roll);
        document.getElementById("dice-content-wrapper");
    
        dice_content_wrapper = createElementWithClass('div', 'dice-content-wrapper');
        outerDiv.appendChild(dice_content_wrapper);
    
        document.getElementById("page-content-wrapper");
    
        dice_toolbar_counter_wrapper_Ul = createElementWithClass('ul', 'dice-toolbar-counter-wrapper');
        dice_toolbar_wrapper_close.appendChild(dice_toolbar_counter_wrapper_Ul);
    
    
        add_remove_roll_func("add","remove","roll");
    
        lielement = createElementWithOutClass('li');
        add_remove_roll.appendChild(lielement);
    
        lielement.appendChild(dice_toolbar_counter_wrapper_Ul);
    
    
        /* <div class="dice-content-wrapper">*/  /*Visar tärningen som kastast*/
        ul_dice_sides = createElementWithOutClass('ul');
        dice_content_wrapper.appendChild(ul_dice_sides);
        document.getElementById("dice-content-wrapper");
        function add_remove_roll_func(){
            for (var i = 0; i<3; i++){
                li_dice_side_one = createElementWithClass('li',arguments[i]);
                add_remove_roll.appendChild(li_dice_side_one);
                document.getElementById("ul");
            }
        }
    
    
    function getNumberForClass(className){
        var diceArray = Array=[ 
        "dice dice-side-one", 
        "dice dice-side-two", 
        "dice dice-side-three", 
        "dice dice-side-four", 
        "dice dice-side-five", 
        "dice dice-side-six"]; 
        return diceArray.indexOf(className)+1;
    }
    
    /*###CREATE DICES ###*/
    function dicesides_func(nr){
        // go to dice-window-wrapper again
        // this could be put in a seporate functon
        var elementToAddDice=" dice-window-wrapper ",
        obj=this, // using this here that's why it's called wiht
        dice,i=0,total=0; // all the dice in this dice window
        // dicesides_func.call to set the right this context
        while((" "+obj.className+" ").indexOf(elementToAddDice)==-1){
            obj=obj.parentNode;
        }
        obj=obj.getElementsByTagName("ul")[2];
        var dicessides = createElementWithClass('li',nr);
        obj.appendChild(dicessides);
        // calculate total (can do in a seporate function)
        dice = obj.getElementsByTagName("li");
        for(i=0;i<dice.length;i++){
            total=total+getNumberForClass(dice[i].className);
        }
        //alert(total);
    
        var wrapper = document.getElementsByClassName("dice-toolbar-counter-wrapper")[0];
    
            while (wrapper.hasChildNodes()) {
        wrapper.removeChild(wrapper.lastChild);
         }
    
         var newelem = createElementWithClass('li',"side-" + total.toString());
                //alert(dice_toolbar_counter_wrapper_Li)
                wrapper.appendChild(newelem);
    
    
    
       function numberTotext(nr){
        var arrClass=["zero","one","two","three","four","five","six","seven","eight","nine"];
        ret=[];
        nr=nr.toString();
        for(i in nr){
            ret.push(arrClass[nr[i]]);
        }
        console.log(ret);
        return ret;
    }
    numberTotext(total)
    /*
            function turnCounter(){       
            for (var i = 0; i < arguments.length; i++){ 
                dice_toolbar_counter_wrapper_Li = createElementWithClass('li',arguments[i]);
                //alert(dice_toolbar_counter_wrapper_Li)
                dice_toolbar_counter_wrapper_Ul.appendChild(dice_toolbar_counter_wrapper_Li);
                document.getElementById("ul");
    
    
    
            } 
    
    
    
        }   turnCounter(ret);*/
    }
    function AddEvent(){ 
            var AddEvent = "add"; 
            var addClass= document.getElementsByClassName(AddEvent); 
            addClass=addClass[addClass.length-1]; 
            addClass.addEventListener("click", addDiceEvent, true); 
            var diceArray = Array=[ 
            "dice dice-side-one", 
            "dice dice-side-two", 
            "dice dice-side-three", 
            "dice dice-side-four", 
            "dice dice-side-five", 
            "dice dice-side-six"]; 
            <!-- console.log("when set",addClass); -->
            function addDiceEvent() { 
                var rand = diceArray[Math.floor(Math.random() * diceArray.length)];
                dicesides_func.call(addClass,rand);
                      }        
    
        }
        AddEvent(); 
        function RemoveEventDice(){ 
            var removeEvent = "remove"; 
            var removeClass= document.getElementsByClassName(removeEvent); 
            var remove=removeClass[removeClass.length-1]; 
            remove.addEventListener("click", removeDice, true); 
    
            function removeDice(e) { 
                var obj=e.target; 
                var elementToRemove=" dice-window-wrapper "; 
                while((" "+obj.className+" ").indexOf(elementToRemove)==-1){ 
                    obj=obj.parentNode; 
                } 
                var allDiceInWindow=obj.getElementsByClassName("dice"); 
                var diceToRemove=allDiceInWindow[allDiceInWindow.length-1]; 
                diceToRemove.parentNode.removeChild(diceToRemove); 
            } 
        }
        RemoveEventDice();
     function rollEvent(){ 
            var rollDices = "roll"; 
            var addClass= document.getElementsByClassName(rollDices); 
            addClass=addClass[addClass.length-1]; 
            addClass.addEventListener("click", rollDice, true); 
            var diceArray = Array=[ 
            "dice dice-side-one", 
            "dice dice-side-two", 
            "dice dice-side-three", 
            "dice dice-side-four", 
            "dice dice-side-five", 
            "dice dice-side-six"]; 
            function rollDice(e) { 
        var obj=e.target; 
        var elementToRemove = " dice-window-wrapper "; 
        while((" "+obj.className+" ").indexOf(elementToRemove)==-1){ 
            obj=obj.parentNode; 
        }
        var allDiceInWindow=obj.getElementsByClassName("dice"); 
    
        for(var i = 0; i<allDiceInWindow.length; i++){
            // set the name individually for each die
            var name = diceArray[Math.floor(Math.random() * diceArray.length)]; 
            allDiceInWindow[i].className = name;
        }
    } 
        }
        rollEvent();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe the title is a little missleading but I'd like to ask you for
Ok the title maybe a little confusing. I have a database with the table
My Title to this question is probably a little confusing, but let me try
The question maybe a little confusing, but it's hard to make clear this question
Maybe the title is a little misleading and question could be too, but I'm
Maybe title is a little bit sophisticated, but situation is simpler: In Sublime text
Maybe the title is a little bit obscure, but let me explain... In a
Ok, maybe the title is a little confusing, but what I am trying to
OK so that title sucks a little but I could not think of anything
The title may be a little confusing, but I don't know how else to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.