Hey.
If I write
function func(){...}; document.getElementById("red").onclick=func;
function func(){...}; document.getElementById("red").onclick=func;
, the onclick property will refer not to "null", but to thefunc()
function.
But at the same time, for some reason, the<div id="red"> ...</div>
element in dev tools (the Elements tab, where the elements of the document are shown) does not add the onclick attribute, although in the div object itself the onclick property will not be referenced on "null", and on functionfunction(){...}
.Question: why not added?
As far as I understand, properties and attributes of the same name are tied tightly - if there is a standard attribute, there must be a property of the same name, and vice versa.
Strange it turns out: the property is there, but the attribute of the same name is not.
I used the basic DOM event model level 0 . In this case, the target object is associated with the handler function through the onclick property.If you use the W3C DOM Level 2 event model, the entry changes.
I writefunction func(){...}; document.getElementById("red").addEventListener("click", func, false);
function func(){...}; document.getElementById("red").addEventListener("click", func, false);
.In this case, the onclick property of the
document.getElementById("red")
object will still refer to "null". That is, in the document.getElementById ("red") object there is no property that refers to the handler functionfunc()
.
How does this even work?
How does the target object communicate with the handler function?