www.webdeveloper.com
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
    Posts
    8

    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
    Posts
    45
    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:

    Code:
    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:

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

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by ntk418 View Post
    Code:
    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:
    Code:
    element = document.createElement('input');
    element.onclick = myEventHandler;

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