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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:25:25+00:00 2026-06-15T23:25:25+00:00

I have a div container. If the user clicks on this div container, then

  • 0

I have a div container. If the user clicks on this div container, then this is replaced with an input box.
If the user click on anywhere else on the page other than the input box, then I want the previous div to be shown again.

This is what I have managed to do until now. This works fine only once. This is the javascript code:

$(document).ready(function() {
$(".new-section").click(function(event) {
    $(this).replaceWith('<div class="span8 offset1 create-section"><input type="text" placeholder="Enter the title of the section"></div>');
    event.stopPropagation();
});

$(".create-section").click(function(event) {
    event.stopPropagation();
});

$(document).click(function(event) {
    $(".create-section").replaceWith('<div class="span8 offset1 new-section"><p>Click to add a new section</p></div>');
});
});

This is the HTML code:

<div class="span8 offset1 new-section">
    <p>
    Click to add a new section
    </p>
</div>

After replacing the input with the div, further clicks on the div do not bring the input box again. It seems like the click event is only trigerred once. Basically what I want is:
1. User clicks on div
2. Div replaced with input
3. User clicks outside input
4. Input replaced with div
5. User clicks on div again and the input box comes back.

The cycle repeats. As of now, I have managed to get this working only once. Any idea what I am doing wrong?

EDIT: I do not want to allow the to be shown again, if the user clicks on the . Only if the user clicks on anywhere else in the web page (other than the ), the should be shown again. In other words, a toggle between the and the elements.

  • 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-15T23:25:26+00:00Added an answer on June 15, 2026 at 11:25 pm

    As you are creating the element dynamically you should delegate the event:

    $(document).on('click', '.new-section', function(event) {
    

    However toggling visibility of the elements is more efficient than replacing them.

    <div class="new-section">
        <p>
            Click to add a new section
        </p>
    </div>
    <div class="span8 offset1 create-section">
        <input type="text" placeholder="Enter the title of the section">
    </div>
    

    $(document).ready(function() {
        var $section = $(".new-section"),
            $div = $('div.span8');
    
        $section.click(function(event) {
            $section.hide();
            $div.show().find('input').focus()
        });
    
        $div.find('input').blur(function(event) {
            $section.show();
            $div.hide()
        });
    })
    

    http://jsfiddle.net/34wHG/

    Note that I have used focus and blur events, you can also create a button which generates the section and hide the div element when button is clicked.

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

Sidebar

Related Questions

I have an input box for user to enter any url. Then I would
I have a container (div) with a background image. In this div there is
I have this container div whose width I would like to set according to
I have a div which contains data. I would like when a user clicks
Situation: I have a check box and a div container inside another div container.
I have div called panel that itself contains another div. When a user clicks
I have a div that contains another div. If the user clicks the inner
I have a div Container, and I am creating a dynamic textbox control when
I have a DIV container with several inner DIVs layed out by floating them
I have a container div (red) with a top padding of 400px. The content

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.