Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.remove is not a function

This question may be duplicate of any. But, after lot of try I couldn't find the proper solution for this.

This is my code

@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
<script>
    $(document).ready(function () {
        var els = document.querySelectorAll('body > *');
        els[els.length - 1].remove(); //getting error here            
    })
</script>

I don't know why my application is showing error in browser console like

TypeError: els[els.length - 1].remove() is not a function

When i can run same function in browser console window and it works. but, when i place my code in the page it shows me error like above. I have also tried to call .removeNode(boo) method but, it was also not working. Actually when i try to write ele[].remove() in the code the intellisence doesn't suggest me that function.

like image 477
Shell Avatar asked Sep 21 '14 03:09

Shell


People also ask

Is not a function TypeError?

A TypeError: "x" is not a function occurs when a function is called on an object that does not contain the called function. When calling a built-in function that expects a callback function argument, which does not exist. When the called function is within a scope that is not accessible.

Is used to delete an html element from the DOM?

remove() The Element. remove() method removes the element from the DOM.


1 Answers

DOMNodes don't have a remove() method. Use this:

$(document).ready(function () {
    var els = document.querySelectorAll('body > *');
    $(els[els.length - 1]).remove(); 
});

or even better, this:

$(document).ready(function () {
    $('body > *').last().remove();
});
like image 62
JLRishe Avatar answered Oct 09 '22 21:10

JLRishe