JS dynamic json array creation is not working in IE 11

I have to make a dynamic JSON array using js. My code is like

var item_id=document.getElementById(vendor_id+"item-id"+i).innerHTML;
     var quantity=document.getElementById(vendor_id+"quantity"+i).value;
     var unit_price=document.getElementById(vendor_id+"unit-price"+i).innerHTML;
     var sub_total=document.getElementById(vendor_id+"price"+i).innerHTML;
     item={
      item_id:item_id,
      quantity:quantity,
      price:unit_price,
      sub_total:sub_total
     }
     console.log(item.item_id);
     products.push(item);

When I run this on chrome, I am getting the item array, also getting the console print item_id.
But when I run this on IE 11 the console print is undefined

item.item_id is undefined

How can I change this to work in all browsers?

2 thoughts on “JS dynamic json array creation is not working in IE 11”

  1. Have you tried logging vendor_id and i? Maybe the string for getElementById is different from what you expected and selecting the element is not working.

    On the other hand, are you sure you want to optimize your website for IE11? I think Microsoft will drop support this year. It probably isn‘t worth the effort.

    Reply
  2. Possibly you should declare item first like:

    var item = {
          item_id:item_id,
          quantity:quantity,
          price:unit_price,
          sub_total:sub_total
         }
    
    Reply

Leave a Comment