Function js does not run in chrome, due to window.event

1

I read something about srcElement and saw that when I do window.event.srcElement, this approach works only in IE. Well, I do not have enough money to discuss this. What happens is that a function I have here does not work in chrome. Below the function:

function MarcarCelula() {
    celula.selecionaCelulaViaTD(window.event.srcElement);
}

There are two errors:

  

1) Uncaught ReferenceError: cell is not defined

     

2) Uncaught TypeError: Can not read property 'value' of null

The reference I took to assert my above hypothesis was this link of SOen

    
asked by anonymous 26.08.2015 / 19:14

1 answer

2

window.event and window.event.srcElement are very specific to IE, in which case you need to perform some checks to make the script compatible with other browsers.

var teste = document.getElementsByName("teste");

var onTesteChange = function (event) {
  event = event || window.event;
  target = event.target || event.srcElement;  

  alert(target.id);
}

for (var indice in teste) {
  teste[indice].onchange = onTesteChange;
}
<input id="teste1" name="teste" type="radio" />
<input id="teste2" name="teste" type="radio" />
<input id="teste3" name="teste" type="radio" />

In case target receives Element that fired the event.

    
26.08.2015 / 19:27