Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.remove() does not remove element from the dom

Tags:

jquery

I want to remove an element from the dom using its id

HTML

<div id="no-js">
     Sorry for the inconvenience<br/>
     Your browser does not support javascript or it must be  disabled.<br/>
     To access the features of our website<br/><br/>
     Enable your javascript or upgrade to the browser that supports javascript.
</div>

jQuery

 var x = $("#no-js"); 
 console.log(x.html());
 x.remove(); // does not remove the element from dom why?
 console.log(x.html()); // it displays html content inside it
 console.log(x.length); // results 1 
 x.empty(); // removes the child elements, working as expected
 console.log(x.html()); // html content is empty
 console.log(x.length); // length is 1

.remove() does not seem to work as expected ? This is the fiddle http://jsfiddle.net/QGsza/14/

like image 780
user3205479 Avatar asked Dec 01 '25 04:12

user3205479


1 Answers

.remove() removes the element from the dom, but still the variable x holds a reference to it that is the reason for x.length to return 1

If you test again console.log($("#no-js").length) it will give 0 as it triggers a new dom search and the element is already removed

Demo: Fiddle

like image 113
Arun P Johny Avatar answered Dec 02 '25 17:12

Arun P Johny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!