Results 1 to 3 of 3

Thread: How to pass a event to the function when element is created using DOM

  1. #1
    Join Date
    Dec 2010

    How to pass a event to the function when element is created using DOM

    Hi All,

    Am creating text input element dynamically using DOM and i want to pass a event when onchange event is triggered.. Am able to assign the function to it but failing to pass the event...

    Please Help me Anybody if u know.....

    Thanks in advance

  2. #2
    Join Date
    Jul 2009
    You want your function to be invoked when the object is created? Or do you mean when it is attached to the body?

    It wouldn't make since the first way since in order to assign any listeners (onchange, onload, onmousedown, etc) to an object it must be created already.

    If you did it this way:

    element = document.createElement('type');
    element.onload = function_to_call;
    It should call the function once it's been appended to the body. Haven't tested it to be sure, but it sounds good

    If you need to pass parameters to the function it would be done this way:

    element.onload = function {
        function_to_call(param1, param2);
    Good luck, let me know if that doesn't work.

  3. #3
    Join Date
    Jul 2008
    urbana, il
    Quote Originally Posted by ntk418 View Post
    element.onload = function {
        function_to_call(param1, param2);
    onload only works for documents, images, and sometimes scripts.

    but your methodoglogy is close to solving the OP's problem.
    you just need to bind the onclick instead of onload:
    element = document.createElement('input');
    element.onclick = myEventHandler;
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Recent Articles