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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:24:49+00:00 2026-05-26T00:24:49+00:00

I am new to jQuery as well as Web development. :-) I want to

  • 0

I am new to jQuery as well as Web development. 🙂

I want to create a questions creation page. It has a Question Text Box and a Add Button.
When users press the add button, there will be a new Question Text Box and a new Add Button appear in the next line. But the old Add Button will become a Delete Button and change its functionality.

Here is jQuery code:

$(function () {
        $(".buttonMinus").hide();

        $(".buttonPlus").click(function (event) {
        $(this).remove(".buttonPlus");
        $(".buttonMinus").show();
        $("#p1").append("<p id='p2'>Question: <input type='text'><button type='button' class='buttonPlus'></button></p>");   **//  Here !!!!**
        alert("Welcome " + $(".ttt").val() + "!");        
    });

    $(".buttonMinus").click(function (event) {
        alert("You have pressed the DELETE button!");
    });

});

And here is the html code:

<body>
<div class="demo">
    <p id="p1">Your name: <input type="text" class="ttt"/>
    <button class="buttonPlus" type="button"></button>
    <button class="buttonMinus" type="button"></button></p>
    <br />
</div>      
</body>

I put all the Add Buttons and Minus Buttons into two different classes, “.buttonPlus” and “.buttonMinus”.
The .buttonPlus works well when the line is added by the main html. But it fails to work when it is added by the .append() function. It simply adds the .buttonPlus’s CSS code to its button, but the $(“.buttonPlus”).click(function (event) {…} is NOT attached to the new buttonPlus.

Could you guys tell me why and how I can solve this problem?

Thanks,
Ashley

  • 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-05-26T00:24:49+00:00Added an answer on May 26, 2026 at 12:24 am

    Welcome to jQuery! This is a common issue. The problem is caused by the fact that the new .buttonPlus element does not exist at the time when you first bind the click event to .buttonPlus. That first time you bind the click event, it will only bind to elements that already exist on the page. This is the entire reason why you wrap all your code inside $(function()) — it ensures that it does not run until all the DOM elements have been created.

    There are a few different solutions. The simplest one is to use jQuery’s .live() method, as Ryan suggests. This will automatically bind the specified click event to any new .buttonPlus elements that get created.

    However, while this is the simplest solution, it’s also very computationally expensive. I won’t get into the details here, but using .live() is pretty much never the best option. There is a similar jQuery method called .delegate() which can be used instead. However, while .delegate() is less expensive than .live(), it’s still not the best solution in your case.

    The best solution, as Diodeus alludes to, is to “re-bind these elements when they’re added.” You could do that in the following way:

    function btnClickEvent(btn){
        btn.remove(".buttonPlus");
        $(".buttonMinus").show();
        $("#p1").append("<p id='p2'>Question: <input type='text'><button type='button' class='buttonPlus'></button></p>");
        alert("Welcome " + $(".ttt").val() + "!");
    
        //Re-binding
        var newBtn = $("#p1").find('.buttonPlus:last');
        newBtn.click(function(){
            btnClickEvent(newBtn);
        });
    }
    
    $(function(){
        $(".buttonMinus").hide();
    
        $(".buttonPlus").click(function(){
            btnClickEvent($(this));
        });
    
        $(".buttonMinus").click(function (event) {
            alert("You have pressed the DELETE button!");
        });
    }
    

    As you can see, all we’re doing is putting the code that runs on the click event into a separate function. That allows us to easily bind it to the new button. You would probably want to do a similar thing with your minus buttons as well.

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

Sidebar

Related Questions

I'm very new to both the Mvc framework as well as JavaScript and JQuery.
I am very new to jQuery and javascript. I have an autocomplete box setup
To start let me just say that I am new to jQuery as well
I have a quick question regarding to mobile development, I'm new at this, so
I'm trying to make an AJAX enabled web page using rails and jQuery. The
Possible duplicate: jquery change div text The above question is a possible duplicate, but
Disclaimer: I'm new to web development. I'm posting information from a CodeIgniter based form
Hi guys I´m new at stackoverflow and also new at Jquery Well hope I
When writing a new jQuery plugin is there a straightforward way of checking that
Using jQuery UI 1.8rc3 combined with the new jquery.effects.fade.js code, I've been able 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.