.remove is not a function

This question can be a duplicate of anyone. But after many tries, I couldn't find a suitable 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 an error in the browser console like

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

      

When I can run the same function in the browser console window and it works. but when i post my code on the page it shows me an error as above. I also tried calling the method .removeNode(boo)

, but it also didn't work. Actually, when I try to write ele[].remove()

in code, intellisence doesn't offer me this feature.

+3


source to share


1 answer


DOMNode

don't have a method remove()

. 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();
});

      

+5


source







All Articles