Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove Div elements in javascript using their id? [duplicate]

Tags:

javascript

dom

Possible Duplicate:
JavaScript: remove element by id

I only know their respective ids and don't know specifically about their parent nodes....

like image 294
NOVICE TANUJ Avatar asked Jun 18 '12 10:06

NOVICE TANUJ


People also ask

How to remove element by id in JavaScript?

You can remove element by Id in javascript using the document.getelementbyid("element_id").remove() method. In this tutorial, you’ll learn how to remove elements by id using different methods and when appropriate to use those methods. Webpages are made up of DOM elements. According to DOM, every HTML tag is an object.

How to remove a specific ‘div’ element using plain JavaScript?

In this article, we will discuss three simple ways to remove a specific ‘div’ element using plain Javascript. Using parentNode.removeChild (): This method removes a specified child node from the DOM tree and returns the removed node. Example: This example uses the parentNode.removeChild () method to remove a specific ‘div’ element.

How do I remove a specific element from a HTML page?

Hence we can remove a specified ‘div’ element by setting its contents to “” using the outerHTML property. Example: This example uses the outerHTML attribute to remove a specific ‘div’ element. Using .remove (): This method removes the specified div element and all its child nodes.

What is the use of Id Id in HTML?

ID is the property used to identify the web page elements, and each element has a unique id across the document. Get the element by using its ID and invoke the remove() method in that element.


2 Answers

You can use parentNode on the element to get its parent, and use removeChild on it.

var el = document.getElementById( 'id' );
el.parentNode.removeChild( el );
like image 148
Florian Margaine Avatar answered Nov 15 '22 07:11

Florian Margaine


In jQuery, just $('#your_id').remove(); will work.

like image 25
xdazz Avatar answered Nov 15 '22 07:11

xdazz