www.webdeveloper.com
Results 1 to 5 of 5

Thread: set onchange event

  1. #1
    Join Date
    Mar 2006
    Posts
    9

    set onchange event

    Hello,

    Probably a stupid question, but how do I set the onchange event of an element I have created in javascript?

    var nextInput = document.createElement('input');
    nextInput.setAttribute('name', 'picture[]');
    nextInput.setAttribute('type', 'file');

    I would like the nextInput element to have an onchange attribute.

    I tried
    nextInput.setAttribute('onChange', 'myFunction');
    but this does not work.

    Any ideas?

    Thanks,

    Smintheu

  2. #2
    Join Date
    Dec 2004
    Posts
    8,637
    Quote Originally Posted by Smintheu
    I tried
    nextInput.setAttribute('onChange', 'myFunction');
    but this does not work.
    onchange is not an attribute -- it is an event. One way to do it is as follows:

    nextInput.onchange = myFunction;

    Note the lack of () on the function. This is on purpose.

  3. #3
    Join Date
    Mar 2006
    Posts
    9
    That did the trick, thanks a lot!

    Smintheu

  4. #4
    Join Date
    Mar 2006
    Posts
    9
    how then do you pass arguments with this function?

    Thanks,

    Smintheu

  5. #5
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    nextInput.onchange = function(){myFunction(this);};

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