Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - remove specific element from cloned node

Tags:

jquery

Im trying to clone tree, remove one element from it and then append result to new place. But the problem is that element is not deleted, it always append original tree.

$(".js-parent-trigger").click(function() {        
    var commentForm = $("#js-add-comment-wrapper").clone(true).css("margin", "10px").remove(".js-add-comment-title");
    $(this).parents(".js-comment-wrapper").append(commentForm);
    return false;
});
like image 932
user313382 Avatar asked May 25 '10 15:05

user313382


People also ask

How to remove an element using jQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.

How to remove appended element in jQuery?

length > 0){ var anotherToDo = $("<p>" + newToDo + "</p>"); $("#toDoList"). append(anotherToDo); $("#newToDo"). val(" "); // later when you want to delete it anotherToDo. remove(); } }); });

How to remove an element from a list in jQuery?

In jQuery, in order to remove element and content you can use anyone of the following two jQuery methods: Methods: remove() – It is used to remove the selected element (and its child elements). empty() – It is used to removes the child elements from the selected element.


2 Answers

$(".js-parent-trigger").click(function() {        
    var commentForm = $("#js-add-comment-wrapper").clone(true).css("margin", "10px");
    commentForm.find(".js-add-comment-title").remove();
    $(this).parents(".js-comment-wrapper").append(commentForm);
    return false;
});
like image 104
user113716 Avatar answered Oct 13 '22 14:10

user113716


Couldn't help making it one big statement:

$(".js-parent-trigger").click(function() {        
   $("#js-add-comment-wrapper").clone(true).css("margin", "10px")
         .find(".js-add-comment-title").remove()
         .end().appendTo('.js-commet-wrapper');
    return false;
});
like image 22
Dan Heberden Avatar answered Oct 13 '22 12:10

Dan Heberden