-2

This code works in modern browsers - but is not working in the IE7. Can someone help me where the problem lies:

function makePay() {
            var obj = {
                            'product' : 'speakers',
                            'paid' : 300,
                            'toBePaid' : 560,
                            'paymentsLeft' : 3,
            }              

            return (obj.toBePaid - obj.paid) / obj.paymentsLeft;
}

var btn = document.getElementById("result");
btn.addEventListener("click", function(){
            alert(makePay());
}, false);
mpavlovic89
  • 749
  • 3
  • 16
  • 37

1 Answers1

2

The reason for IE 7 failing is the addEventListener method.

IE versions older than 9 have their own, proprietary method for attaching event handlers. You’ll need to check if addEventListener is supported and if it’s not the case, check for attachEvent and use this instead.

Example (taken from https://stackoverflow.com/a/6927800/1387396):

function addEvent(evnt, elem, func) {
   if (elem.addEventListener)  // W3C DOM
      elem.addEventListener(evnt,func,false);
   else if (elem.attachEvent) { // IE DOM
      elem.attachEvent("on"+evnt, func);
   }
   else { // No much to do
      elem[evnt] = func;
   }
}

Good luck! I feel with you.

Community
  • 1
  • 1
aaronk6
  • 2,701
  • 1
  • 19
  • 28