javascriptjqueryequivalent

What is the equivalent of jQuery's .before() function in Javascript?


What is the equivalent of .before() in Javascript?


Solution

  • node.insertBefore() is pretty much the equivalent : https://developer.mozilla.org/en-US/docs/Web/API/Node.insertBefore

    $('#id').before('something');
    //equivalent 
    var node = document.getElementById('id');
    node.parentNode.insertBefore('something', node);
    

    Here what jQuery does : https://gist.github.com/kagagnon/a13de27760ba1af883c0#file-gistfile1-js-L6064

    before: function() {
        return this.domManip( arguments, function( elem ) {
            if ( this.parentNode ) {
                this.parentNode.insertBefore( elem, this );
            }
        });
    }