prepend/append works in Chrome and Firefox but not IE11 and Edge

creeser picture creeser · Feb 23, 2018 · Viewed 13.8k times · Source

Trying to prepend data inside a text box in Chrome and Firefox works. Get error: SCRIPT438: Object doesn't support property or method 'prepend' in IE11 and Edge. Thx

    function init_TGs(){
        if (confirm("Initialize TinyG's?")){
            $.ajax({
                type: 'POST',
                url: "init_TGs", 
                data: 'None',
                success: function(result){
                    if (result != ''){
                        var rslt= result;
                        var item = document.getElementById('TextArea1');
                        item.prepend(rslt);
                    }}
                });
            }};

Answer

Sergiu picture Sergiu · Apr 19, 2019

Or, instead of adding a new polyfill you can use insertBefore function which is supported by all browsers:

var rslt= result;
var item = document.getElementById('TextArea1');
item.insertBefore(rslt, item.childNodes[0]);