Link to home
Start Free TrialLog in
Avatar of Star79
Star79Flag for United States of America

asked on

document.addEventListener doesnt fire in IE 9 or any other IE versions

Hello,

I have a situation here, we want to copy the contents of grid (written in MVC) to clipboard and then copy it to Excel (using CTRL-V). So when I click on COPYGRID button on UI the following function copyGrid()  is called but the eventlistener (document.addEventListener) doesn't get fired. Your help is highly appreciated as this issue is kind of urgent.


Here is the code
-----------------------
document.addEventListener('copy', function (e) {
  console.log(e);
  try{
  alert("starting to copy to clipboard");
        e.clipboardData.setData('text/html', searchString);
        e.preventDefault();
        alert("Table copied to clipboard");
    } catch (e) {
      alert("Issue copying table to clipboard");
   }
});

function copyGrid() {
    document.execCommand('copy', false, copyGrid);

}


Regards,
Rithesh
Avatar of Star79
Star79
Flag of United States of America image

ASKER

Why this question is neglected?
Avatar of Randy Downs
Maybe this will help.

common practice is to check if the addEventListener method is available and use it, otherwise use attachEvent:

function bindEvent(el, eventName, eventHandler) {
  if (el.addEventListener){
    el.addEventListener(eventName, eventHandler, false); 
  } else if (el.attachEvent){
    el.attachEvent('on'+eventName, eventHandler);
  }
}
// ...
bindEvent(document.getElementById('myElement'), 'click', function () {
  alert('element clicked');
});

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial