Solved

Removing DOM elements with jQuery

Posted on 2008-10-04
3
17,625 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

733 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