View Full Version : creating event

12-25-2004, 08:37 AM
I am trying to create an Event to add it to a html tag. I thought that the
answer was createEvent("MouseEvents") but it was not the case. createEvent doesn't work the same way as a createElement. It simulate an event but don't write the event.

What I am looking for is a function wich has the ability to write the event
as such:

and the result must be as such <div id="mydiv" onmousedown="abc(x,y,z)">

addEventListener doesn't help me because we can not add the function
(this) and if we made a save of the complete page we see that the event was not written. In the case of createElement the object would have been
written in the page. The other problem with addEventListener, it executes
once we call it.

I know that for the Java DOM there is object.setEvent("onclick", "abc(this") )

I am looking for something which is able to write the script
"physically" on the page. In such way when you save the page you can find
the information you have created.

What I am looking is a kind of setAttribute because all the code
which refer to UIEvent, dispatchEvent, initEvent, initMouseEvent... manage
the behavior of the event virtually and the creatEvent doesn't write the
event it is more like a punctual event.

I hope I succeeded to make myself understand.

would someone have an Idea
Thanks in advance

happy hollidays


12-25-2004, 09:25 PM
Just do an element.setAttribute("onclick", "some javascript to execute"); then.

12-25-2004, 11:54 PM
Thank You for the information.

I have never thought about that. The best is to use createAttribute and setAttributeNode to be sure to be compatible between the browsers.

To my opinion I think DOM is not always consistant and make us confuse.

The problem is what we consider as an attribute. For myself it was only for creating style attribute inside in the htlm tag.
Then class is an attribute, width and heigh of an image are attribute, src is an attribute.....

Then why using attribute if we can write


even it haven't this attribute.

Then we probably can write document.getElementById("div").onmouseover="abc(this)"

Why the standard don't use the createEvent instead of the createAttribute?

Maybe you can explain why then using createEvent?

Thanks in advance


12-26-2004, 01:14 AM
For starters, for most compatibility, setAttribute() is much better than createAttribute() and appending stuff that way.

Secondly, I think you are slightly confused on the difference between an attribute and an event handler.

<a href="http://www.google.com" onclick="alert('hello')">Google</a>

What is that bolded part? An event listener? SGML doesn't know about event listeners. It's an attribute. However, the browser has special knowledge of this particular attribute - it has the name "onclick". Therefore it establishes an event handler for you.

You mentioned earlier that createEvent() doesn't establish event handlers. Of course it doesn't, it creates an event object that can be used via dispatchEvent() to synthetically fire events, for instance.

addEventListener() is what establishes a connection between code execution and a listener.

If I do someElement.addEventListener(...), can this be represented by markup? Well, no. What if I'm dealing in MathML which doesn't have the attribute onclick? It still makes sense to click on an equation though. An XML serializer cannot serialize event handlers because they are not a part of the markup layer of a content. Presentation, perhaps. But most would argue it's part of the behavior layer, which is not necessarily persistent.

So we come back to someElement.setAttribute("onclick", "...");

In this case, we are altering the markup, not the behavior. Fortunately for us, the browser automatically sets up the appropriate event listeners for us. setAttribute also creates and adds (or replaces) the attribute in the markup, which will be serialized when it converted.

12-26-2004, 03:42 AM
Thank you for your information

You are the first one who explain the right way event (I have read a lot of article about event). Now It make more sens for me. For myself DOM was done only for XHTML and HMTL, I forgot that DOM is for all the web language.
You are write for the setAttribute. Site which are reference made many mistakes. I still prefer looking to microsoft reference which give the best explanation and after I looked if it is support by firefox and standard an use it And effectivly setAttribute is the best choice.
The best thing I have to do it is to by the book of Joe Marini which seems the best one about DOM, because in the O'reilley books I found mistake in their examples. It seems very difficult to found the right information. The w3c document is for my opinion not well done to understand well how everything works and browsers have their own properties which made things more complex espcially when you are building a software using javascript.

Thanks you again and sorry for my poor english which is not my language.