Solved

Removing DOM elements with jQuery

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
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…

687 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