Solved

Removing DOM elements with jQuery

Posted on 2008-10-04
3
17,619 Views
Last Modified: 2012-06-22
I don't  quite understand how the "remove" method of jQuery is working. I'm trying to remove a HTML element from a DOM node. I have appended this element by using jQuery like this:

    element = $("#templates .window").clone(true);
    $("#desktop").append(element);

Then I try to remove this appended element with this line ("element" is the same object as used in the append-line above):

    $("#desktop").remove(element);

But the result is an error message:

Error: t.substring is not a function
Source File: http://localhost/~k/x3tc/scripts/jquery.js
Line: 1681

In other jquery methods I can use selector expressions and DOM nodes as arguments. But looks like the "remove" method only accepts selector expressions. So how can I remove this node from the DOM? The only reference I have is this "element" variable.
0
Comment
Question by:kayahr
3 Comments
 
LVL 1

Accepted Solution

by:
zivperry earned 500 total points
ID: 22642173
jQuery.remove() method, removes the object it refer to.
So, to remove the element you need to use it this way:

element.remove();

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22642505
if you were interested in removing #desktop, then you would need to use:
$("#desktop").remove();

in that statement, $("#desktop") first returns a reference to the DOM object that represents #desktop, and once the reference it returned, the remove() method is executed. In your case, the variable "element" already has a reference to the node you wish to remove, so all you need is:
element.remove();

0
 
LVL 7

Author Closing Comment

by:kayahr
ID: 31503081
So I just thought to complicated and there was a much easier way. Thanks :-)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now