2
In answering that question, I came across the following situation. Note that onclick
in x
is an object with two methods, when the element receives the click executes the method click
which in turn was to execute the method funcao_a
, but returns the following error:
Typeerror: x.onclick.funcao_a is not a Function at Htmlheadingelement.click
var x = document.getElementById("title");
x.onclick = {
funcao_a: function() {
console.log('Função A');
},
click: function() {
console.log('Click');
x.onclick.funcao_a();
}
};
x.onclick = x.onclick.click;
<h1 id="title">Somente alguns testes em JS</h1>
But it doesn’t happen the same when I have an object with a different name, as for example: x.objeto
.
var x = document.getElementById("title");
x.objeto = {
funcao_a: function() {
console.log('Função A');
},
click: function() {
console.log('Click');
x.objeto.funcao_a();
}
};
x.onclick = x.objeto.click;
<h1 id="title">Somente alguns testes em JS</h1>
My question is: Is it possible to access this method as in the first example? If yes how ?
So far I understood, but still the error persists. Note that I changed the question, calling
x.onclick = x.onclick.click;
when performing the functionclick
the functionfuncao_a
call on it does not work. Returns error.– NoobSaibot
Now the problem is slightly different. For it first stores the object with
x.onclick = { .... };
. But then take the object out and just put the function withx.onclick = x.onclick.click;
, leaving the previous object again inaccessible. At the end of the day ifonclick
it’s function will not be able to doonclick.qualquercoisa()
. If it’s still not clear, just let me know.– Isac
In this case only the second example would work correctly. Thanks for the clarification, it didn’t even cross my mind.
– NoobSaibot
@wmsouza Just the second example is that it works correctly. I am glad to have helped.
– Isac