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.
The jQuery remove() method removes the selected element(s) and its child elements.
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.
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.
To clear the contents of a div element, set the element's textContent property to an empty string, e.g. div. textContent = '' .
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With