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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:46:16+00:00 2026-06-02T09:46:16+00:00

I am using asp.net MVC 3, and I am trying to remove a LI

  • 0

I am using asp.net MVC 3, and I am trying to remove a LI that was added through a HTTP Post.

I am able to remove the item that already in the UI but not the new item got added.

Here is UI code:

<a href="#" id="addNew">Add New</a>
<div id="ulList">
    <ul>
        <li id="li_1"><a href="#" id="a_1" class="aLink">1</a></li>
        <li id="li_2"><a href="#" id="a_2" class="aLink">2</a></li>
        <li id="li_3"><a href="#" id="a_3" class="aLink">3</a></li>
    </ul>
</div>
<script type="text/javascript">

    $('#addNew').on("click", function () {
        $.ajax({
            url: '@Url.Action("AddItem")',
            type: "POST",
            data: { id: 4 },
            success: function (data) {
                $('#ulList ul').append(data);
            }
        });
    });

    $('.aLink').on("click", function () {
        RemoveItem($(this).attr('id'));
    });

    function RemoveItem(id) {
        id = id.substring(id.indexOf("_") + 1);
        $.ajax({
            url: '@Url.Action("RemoveItem")',
            type: "POST",
            data: { id: id },
            success: function (event, ui) {
                $('#li_' + id).remove();
            }
        });

    }
</script>

The AddItem calls teh AddItem in code behind and returns simple string.

[HttpPost]
        public ActionResult AddItem(int id)
        {
            return Content(@"<li id=""li_4""><a href=""#"" id=""a_4"" class=""aLink"">4</a></li>");
        }

The Item does get added, but notice that when you click on the new added item “4”, it doesn’t get removed or the call is made to the Remove the item. Please advice to what I need to do in order for this to work.

  • 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-02T09:46:18+00:00Added an answer on June 2, 2026 at 9:46 am
    $('.aLink').on("click", function () { 
        RemoveItem($(this).attr('id')); 
    }); 
    

    Your jQuery event handler will only attach events to elements that were initially on the page with .on. In order for your events to work on dynamicly added elements you have 2 options depending on what version of jQuery your running.

    V1.4 -> 1.7 .delegate()

    $('#ulList').delegate("click","a" function () { 
        RemoveItem($(this).attr('id')); 
    });
    

    v1.7+ amended .on() -> retarget to point to parent container

    $('#ulList').on("click","a", function () { 
        RemoveItem($(this).attr('id'));
    }); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an ASP.NET MVC application, but I'm trying to remove everything but
I am trying to create a Repository using the following tutorial http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application I get
I'm trying to create a simple CalDav server using ASP.NET MVC 3, but the
I am using Asp.Net MVC 2, and trying to iterate through the metadata to
Using ASP.NET MVC when trying to get the information stored on my Session[objectName] from
I'm using ASP.NET MVC Framwork and trying to grok the ASP Membership 3.5 stuff.
Using Asp.Net MVC 3 (Razor). I'm trying to add the blockUI plug in to
I'm trying to write some code using pure SQL using ASP.NET MVC. I assume
I am trying to create a drop down list using asp.net mvc. Model: public
I am using JW Player in an ASP.NET MVC application and I'm trying 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.