Click here to Skip to main content
16,019,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on MVC5 Project, there I am dynamically creating html.

Using jquery,

JavaScript
$("#divbindProjectlist").append('<a href="#" class="list-group-item projectList" ><span class="project-name">' + json.ProjectName + '<input type="hidden" value="' + json.idPartnerProject + '" /></span><i class="pull-right close-p">x</i></a>');


View,

HTML
<div id="divbindProjectlist" class="left-top-p">
    <a class="list-group-item active left-menu-head">Projects List</a>
    <a href="#" class="list-group-item projectList">
        <span class="project-name">ssssssss</span><i class="pull-right close-p">x</i>
        <input type="hidden" class="hidprojectId" value="0.37421775864182916">
    </a>
    <a href="#" class="list-group-item projectList">
        <span class="project-name">asdfasdf</span><i class="pull-right close-p">x</i>
        <input type="hidden" class="hidprojectId" value="0.6032844290071158">
    </a>
    <a href="#" class="list-group-item projectList">
        <span class="project-name">asdfasdf</span><i class="pull-right close-p">x</i>
        <input type="hidden" class="hidprojectId" value="0.28981124419382853">
    </a>
</div>


What I have tried:

Now, How can I remove the each 'a' element when click on x ?

I have tried this. But not working.

JavaScript
$(document).on("click", ".close-p", function () {
       $(this).parentsUntil(".list-group-item.projectList").remove();
   });
Posted
Updated 11-May-17 1:55am
v2

Try:
JavaScript
$(document).on("click", ".close-p", function (evt) {
    evt.preventDefault();
    $(this).closest(".list-group-item.projectList").remove();
});

Demo[^]
 
Share this answer
 
Comments
Member 12955507 11-May-17 8:00am    
Thank god! Respected Sir, You have saved me... thank a lot...
If you are creating html dynamically then its better to give a id to inner content and delete that content by the help of that id. On every delete button append a jquery function (Delete(youdd)) and remove the content on basic of that id.

Ex:

function Delete(id) {
$("#"+id).remove(); //
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900