0

This code works well in modern browsers. What should be done to make it working in IE7?

function paymentCheck() {
            var obj = {
                            'type' : 'car',
                            'year' : 2008,
                            'make' : 'Dodge',
                            'model' : 'Avenger',
                            'payments' : 'in process',
                            'paid' : 13286,
                            'toBePaid' : 34856,
                            'paymentsLeft' : 24,
            }              

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

var button = document.getElementById("calcButton");
button.addEventListener("click", function(){
            alert(paymentCheck());
}, false);
j08691
  • 204,283
  • 31
  • 260
  • 272
Messi
  • 97
  • 7

2 Answers2

1

IE7 don't have addEventListener function. You should use attachEvent, but this is IE8 and less only, other browsers don't support it! See MSIE and addEventListener Problem in Javascript?

Community
  • 1
  • 1
Akxe
  • 9,694
  • 3
  • 36
  • 71
0

As Akxe mentioned IE7 does not support the addEventListener function. You can check if the browser supports addEventListener and if not, use the attachEvent instead:

// check if the browser supports 'addEventListener'
if(document.addEventListener){ 
    button.addEventListener("click", function(){
        alert(paymentCheck());
    });
} else {
    button.attachEvent("click", function(){
        alert(paymentCheck());
    });
};
cnorthfield
  • 3,384
  • 15
  • 22