6

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);
                    }}
                });
            }};
Musa
  • 96,336
  • 17
  • 118
  • 137
creeser
  • 363
  • 1
  • 4
  • 11

4 Answers4

24

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]);
Sergiu
  • 1,397
  • 1
  • 18
  • 33
6

IE and Edge does not support the javascript prepend method, so to make prepend method to work you need to use polyfill.

(function (arr) {
  arr.forEach(function (item) {
    if (item.hasOwnProperty('prepend')) {
      return;
    }
    Object.defineProperty(item, 'prepend', {
      configurable: true,
      enumerable: true,
      writable: true,
      value: function prepend() {
        var argArr = Array.prototype.slice.call(arguments),
          docFrag = document.createDocumentFragment();

        argArr.forEach(function (argItem) {
          var isNode = argItem instanceof Node;
          docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
        });

        this.insertBefore(docFrag, this.firstChild);
      }
    });
  });
})([Element.prototype, Document.prototype, DocumentFragment.prototype]);

usage

document.getElementById("some_id").prepend(Element-you-want-to-prepend)

for more info check

https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/prepend#Polyfill

Kaleem Nalband
  • 687
  • 7
  • 21
0

https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/prepend#Browser_compatibility Prepend doesn't have support for IE and edge. Instead of Prepend just try to rewrite the business logic with some other function.

thatcoder
  • 367
  • 2
  • 9
-3

ok, use jquery. Simple solution:

$(document).ready(function(){
    $('#button-sel').on('click', function(event) {
        var targ = event.target.id;
        //alert("You clicked on: " + targ);
        $.ajax({
            type: 'POST',
            url: targ, 
            data: 'none',
            success: function(result){
                if (result != ''){
                    var rslt= result;
                    $('#TextArea1').prepend(result);
            }}
        });
});})
creeser
  • 363
  • 1
  • 4
  • 11