?
Solved

Removing DOM elements with jQuery

Posted on 2008-10-04
3
Medium Priority
?
17,640 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
zivperry earned 2000 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

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)
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…
Suggested Courses

743 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