0

I have a script that includes addEventListener and have discovered it isn't supported in < IE9. All the fixes I have seen, do not work when edited according to my script, so looking for some tailored help!

    $(document).ready(function(){




var excuses = [

'excuse one',
'excuse two',
'excuse three',
'excuse four',
'excuse five',
'excuse six',
'excuse seven',
'excuse eight',
'excuse nine'

];

var bttn = document.getElementById('refresh');
var excuse = document.getElementById('excuse');
var rando = 0;

var makeExcuse = function(){
  rando = Math.floor(Math.random()*excuses.length);
  excuse.innerHTML = excuses[rando];
};



makeExcuse();

bttn.addEventListener('click',function(e){
  e.preventDefault();
  makeExcuse();






});



});

Thanks a lot!

Nils Larson
  • 488
  • 4
  • 14
user2144363
  • 37
  • 2
  • 5

2 Answers2

3

Try this:

if(!bttn.addEventListener) {
    bttn.attachEvent("onclick", makeExcuse);
}
else {
    bttn.addEventListener("click", makeExcuse, false);
}

You must do this for IE8 and older because it is not supported in versions older than IE 9. It is documented in this, about halfway down the page.

Legacy Internet Explorer and Attach Event

Tricky12
  • 6,752
  • 1
  • 27
  • 35
0

Just do it the jQuery way:

 $('#refresh').bind('click', makeExcuse);
Todd Bellamy
  • 152
  • 3