Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: creating event

  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    creating event

    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:
    el=CreateEvent("mousedown")
    el.eventFunction="abc(z,y,x)"
    HTMLelement("mydiv").append(el)

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

    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

    archimede

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Just do an element.setAttribute("onclick", "some javascript to execute"); then.

  • #3
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    setAttribute for creating Event

    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

    document.getElementById("div").style.backgroundColor="black"

    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

    archimede

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    event part2

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •