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 9283711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:42:02+00:00 2026-06-18T18:42:02+00:00

I need to create one button that will cycle through 3 states . For

  • 0

I need to create one button that will cycle through 3 states. For some reason that is incredible challenging to find. The functions in each state are simple style changes, example:

click 1: hide div
click 2: show hidden div, change bg-color
click 3: reset bg-color, change font
click 1: reset font, hide div

Any ideas? I can’t use jquery (class assignment, not allowed)

  • 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-18T18:42:03+00:00Added an answer on June 18, 2026 at 6:42 pm

    Cou could use an array, with a function for each state change, and a counter variable which cycles through the possible states.

    Then simply invoke the current state function through an click handler of the button.

    Something like this could do it

    var toggle = (function (el) {
        var div = document.getElementById(el); //Get the div you want to change
        var states = []; //The Array to hold the functions
        var style = {} //Will be used to save the current style
        for (var att in div.style) //Saves the style of the div *I was just lazy and did a for loop*
        style[att] = div.style[att]
    
        var current = 0; //The Counter
    
        states[0] = function () { //The first state function
            div.style["font-family"] = style["font-family"]
            div.style.display = "none";
        };
        states[1] = function () {
            div.style.display = "block";
            div.style["background-color"] = "rgb(" + [rand(), rand(), rand()] + ")"; // [1,2,3] toString is "1,2,3"
        };
        states[2] = function () {
            div.style["background-color"] = style["background-color"];
            div.style["font-family"] = "Courier New";
        }
    
        function rand() { //ONly to return a random number for a random bgcolor
            return ~~(Math.random() * 255)
        }
        return function () {  //The function which cycles through the states
            states[current]() //Invokes the current statechange function
            current = (current + 1) % (states.length); //Increments the counter and uses modulo to cycle
        }
    })("div");
    
    document.getElementById("click")
        .addEventListener("click", toggle);
    

    Heres an example on JSFiddle

    Update:

    I modified it a bit and commented the changed code, this should be able of changing the states of multiple elements on a page

        function rand() {
            return~~ (Math.random() * 255);
        }
    
    
        var makeToggle = function (states, elements) { // I Changed it to makeToggle, The first argument accepts an array of states to cycle through, the second either an array of elements, or an array of objects with the element property (and an optional state function)
            var current = 0; //Sets the counter to zero
    
            for (var i = 0, ilen = elements.length; i < ilen; i++) {
                if (!elements[i].element) { //check if you passed an Object with the `element` Property
                    elements[i] = {
                        element: elements[i] //If it was an array, the arrays element will be set to an object
                    }; //to support arrays only
                }
                elements[i].style = {}; //to save the original style in the object
                for (var att in elements[i].element.style) {
                    elements[i].style[att] = div.style[att]; // saves it
                }
            }
    
            function doForElements() { //Invokes either the state function passed with an element, or the general statefunction
                for (var i = 0, ilen = elements.length; i < ilen; i++) {
                    var state = elements[i].states;
                    if (state && typeof state[current] === "function") state = state[current];
                    else state = states[current];
                    state(elements[i].element, elements[i].style); //Invokes the function with the element as first parameter and the original style as second
                }
    
            }
            return function () { //Returns the function for the click handler
                doForElements();
                current = (current + 1) % (states.length); //cycles the current state counter
            };
        };
    
        var states = []; //Here the General State change functions get defined
        states[0] = function (div, style) {
            div.style["font-family"] = style["font-family"];
            div.style.display = "none";
        };
        states[1] = function (div, style) {
            div.style.display = "block";
            div.style["background-color"] = "rgb(" + [rand(), rand(), rand()] + ")";
        };
        states[2] = function (div, style) {
            div.style["background-color"] = style["background-color"];
            div.style["font-family"] = "Courier New";
        };
    
    
        var elements = [].slice.call(document.getElementsByTagName("div")); //To actually get an Array of the NodeList (all divs on the page)
    
        elements[4] = { //changes the 5th element (which should receive a special statechange function)
            element: elements[4],
            states: {
                1: function (div, style) { //Use an Objects property to pass an single state change instead of an array with functions
                    div.style.display = "block";
                    div.style["background-color"] = "yellow";
                }
            }
        };
    
    
        var toggle = makeToggle(states, elements); //sets the function for the click handler to toggle
        //Pass an Object with the Elements and an optional st
    
        document.getElementById("click")
            .addEventListener("click", toggle); //binds the function
    

    Heres a JSBin to try it out

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a stored proc that will return records (more than one
I need to create a data.frame that will be populated one row at a
I need to create one drop down list contains some Languages. By selecting any
I need to create a single trace file that spans several days for one
I need to create a batch file that reads a file with one line
I'm trying to create a form using textarea and a submit button that will
I'm working on a site that will have more than one 'Save' button per
I need to create one list based on two other lists. But seems like
I have a Report. I need to create another one, similar to the one
I need to create more than one UIView with MARQUEE effect like HTML <marquee>

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.