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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:47:02+00:00 2026-06-03T02:47:02+00:00

Problem: Creating an Element on a button click then attaching a click event to

  • 0

Problem: Creating an Element on a button click then attaching a click event to the new element.

I’ve had this issue several times and I always seem to find a work around but never get to the root of the issue. Take a look a the code:

HTML:

<select>
  <option>567</option>
  <option>789</option>
</select>

<input id="Add" value="Add" type="button">    <input id="remove" value="Remove" type="button">   

<div id="container">
  <span class="item">123</span>
  <br/>
  <span class="item">456</span>
  <br/>
</div>

JavaScript

 $(".item").click(function () {
    if ($("#container span").hasClass("selected")) {
        $(".selected").removeClass("selected");
    }
    $(this).addClass("selected");
});


$("add").click(function() {

   //Finds Selected option from the Select

   var newSpan = document.createElement("SPAN");
    newSpan.innerHTML = choice;//Value from Option
    newSpan.className = "item";
    var divList = $("#container");
    divList.appendChild(newSpan);//I've tried using Jquery's Add method with no success
   //Deletes the selected option from the select
})

Here are some methods I’ve already tried:

  • Standard jQuery click on elements with class “item”
  • Including using the `live()` and `on()` methods
  • Setting inline `onclick` event after element creation
  • jQuery change event on the `#Container` that uses Bind method to bind click event handler

Caveat: I can not create another select list because we are using MVC and have had issues retrieving multiple values from a list box. So there are hidden elements that are generated that MVC is actually tied to.

  • 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-03T02:47:03+00:00Added an answer on June 3, 2026 at 2:47 am

    Use $.on instead of your standard $.click in this case:

    $("#container").on("click", ".item", function(){
      if ( $("#container span").hasClass("selected") ) {
        $(".selected").removeClass("selected");
      }
      $(this).addClass("selected");
    });
    

    It looks to me like you want to move the .selected class around between .item elements. If this is the case, I would suggest doing this instead:

    $("#container").on("click", ".item", function(){
      $(this)
        .addClass("selected")
        .siblings()
          .removeClass("selected");
    });
    

    Also note your $("add") should be $("#add") if you wish to bind to the element with the “add” ID. This section could also be re-written:

    $("#add").click(function() {
      $("<span>", { html: $("select").val() })
        .addClass("item")
        .appendTo("#container");
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I was trying to create Hibernate Validator bean, and run into this problem creating
I am creating an AJAX application. When a user selects a button a new
I've got a bit of a problem creating a navigation element - its working
I have a problem creating a std::map<int, int> from a vector of pointers, called
I have a problem creating the following SQL Statement using LINQ & C# select
I have a problem creating and executing a JAR file. I have already made
I'm having a problem creating a trigger on a database for a project I've
I've been having a problem creating a checklist in the style of the TouchCells
Having a bit of a problem creating an instance of an object. Bear in

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.