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

  • Home
  • SEARCH
  • 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 8455117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:12:08+00:00 2026-06-10T12:12:08+00:00

Hoping someone can help me. I’m trying to create a function which adds a

  • 0

Hoping someone can help me. I’m trying to create a function which adds a div (not the problem).

The problem is that I want the function to check if the div before is div1 or div2, and then add the next.

The pattern should look like this: div1, div2, div1, div2 etc. Right now I have 2 buttons, where the adds div1 and the other adds div2. I need a button, which adds 1 div onClick, but checks which div has already been added and then adds the next div(div1 or div2) inline.

My code so far:

function div1(){
                    var div1 = document.createElement("div");

                    div1.className = "div1";

                    $('.wrapper').append(div1);
                }

                function div2(){
                    var div2 = document.createElement("div");
                    div2.className = "div2";

                    $('.wrapper').append(div2);

                }

                $(".addDiv1").click(function(){
                    div1();
                })
                $(".addDiv2").click(function(){
                    div2();
                })
  • 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-10T12:12:10+00:00Added an answer on June 10, 2026 at 12:12 pm

    Intuitively, I would just create a generic function that changes the class of the div you want to add according to the previous state, and a global variable to keep track of the state you were in:

    var current_class = "div1"
    
    function newdiv(myclass){
      var div = document.createElement("div");
      div.className = myclass;
      $('.wrapper').append(div);
    }
    
    
    $(".addDiv").click(function(){
      newdiv(current_class);
      if (current_class == "div1"){
        current_class = "div2"
      }
      else {
        current_class = "div1"
      }
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hoping someone can help with a VBA problem I am having. I want to
I am hoping someone can help me with a css problem... I am using
I'm hoping someone can help me as I've been stuck on this problem for
I'm hoping someone can help me with this annoying little problem I'm having. I'm
Hi I'm hoping someone can help me out with a jQuery lavalamp problem. I
I'm hoping someone can help me. I've got a specific Exception from COM that
hoping someone can help me out with this. I'm trying to figure out whether
Hoping someone can help with this. I have other VM's that dont have problems,
Im hoping someone can help me with a small problem! I have a php
I hoping someone can help me with a problem I'm stuck(again) with. if i

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.