Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Removing DOM elements with jQuery

Posted on 2008-10-04
3
Medium Priority
?
17,648 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

604 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