Request not working in firefox

1

I am making an Ajax request, where it works in Chrome perfectly however in firefox it does not work.

It tells me that event is not defined

function pegarValor() {
        dado =  event.srcElement.innerText;

        var XMLHttp =  generateXMLHttp();
        XMLHttp.open("get", "classes/getData.php?result=" + dado, true);
        XMLHttp.onreadystatechange = function () {

            if (XMLHttp.readyState == 4)
                if (XMLHttp.status == 200) {
                    data = XMLHttp.responseText.split("#");
                    if(XMLHttp.responseText == ""){

                        $(".conteudo-select").html("Não foi encontrado");
                    }else{
                        $("#txtRazaoSocial").val(data[0]);
                        $("#txtCNPJ").val(data[1]);

                        $(".conteudo-select").fadeOut(500);
                    }
                }
        };
        XMLHttp.send(null);

    }

There in HTML is a

<div onlick='pegarValor()'>

    
asked by anonymous 28.07.2017 / 13:58

1 answer

1

For a long time (I think more than a decade) the Internet Explorer staff has decided that it would be a good idea to have a global variable called event , always populated with event triggered data.

If this was even a good idea, it's a matter of opinion. What is fact is that the main browsers follow this idea, with the exception of Firefox. Source: MDN .

In your case, for the function to work in Firefox, you need to declare it to receive a parameter, like this:

function pegarValor(event) {
    dado =  event.srcElement.innerText;
    // resto do código

And if your function is called by hand and not by capturing an event, you need to mount an object with the data you want and pass it explicitly.

    
28.07.2017 / 14:17