Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove DIV tag using Javascript or Jquery

How do I remove a DIV with a specific value?

<div value="0" class="task_row"></div>

I want to remove the above div which has value 0.

like image 211
Sanjai Palliyil Avatar asked Jan 21 '11 05:01

Sanjai Palliyil


People also ask

How remove appended div in jQuery?

The jQuery remove() method removes the selected element(s) and its child elements.

What is remove in jQuery?

jQuery remove() Method The remove() method removes the selected elements, including all text and child nodes. This method also removes data and events of the selected elements. Tip: To remove the elements without removing data and events, use the detach() method instead.

What is difference between detach () and remove () method in jQuery?

detach() method is the same as . remove() , except that . detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.

How do you clear a div?

To clear the contents of a div element, set the element's textContent property to an empty string, e.g. div. textContent = '' .


1 Answers

As Ben Rowe points out in the comments, value is not a valid attribute of the div tag. And both the jQuery solution and the solution that uses getElementsByTagName() has to iterate through a list, which is bad for performance. I think that creating an id attribute instead is a better option:

<div id="task_row_0" class="task_row"></div>

And then you can just do:

var div = document.getElementById("task_row_" + taskId);
div.parentNode.removeChild(div);
like image 87
Elian Ebbing Avatar answered Sep 30 '22 10:09

Elian Ebbing