js function does not run on Chrome, due to window.Event

Asked

Viewed 351 times

1

I read something about srcElement and saw that when I do window.event.srcElement, this approach works only on IE. Well, I don’t have enough allowances to discuss it. What’s going on is that a function I have here doesn’t work on Chrome. Down with function:

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

Comes two mistakes:

1) Uncaught Referenceerror: cell is not defined

2) Uncaught Typeerror: Cannot read Property 'value' of null

The reference I took to affirm my hypothesis above was in this link soen

  • Enter all the code if possible, because the first error seems to be related to the object celula and not with window.event.

  • Yes srcElement it is only available in IE, for all brownser you can use the target https://developer.mozilla.org/en-US/docs/Web/API/Event/target

1 answer

2


the window.event and the window.event.srcElement are quite specific to IE, in this 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 the case target receives the Element that triggered the event.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.