Search code examples
javascriptinternet-explorer-7

What should be done to make it working in IE7?


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);

Solution

  • 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());
        });
    };