Dynamically Assigning OnChange Handler

Discussion in 'Javascript' started by HugeBob, Jun 3, 2008.

  1. HugeBob

    HugeBob Guest

    Hi All,

    I'm creating textareas via the DOM and I'm trying to add the onchange
    event to them. Here's my code:

    mytextarea = document.createElement("textarea");
    mytextarea.onchange = mymethod;
    mytextarea.name = "textarea1";
    mytextarea.value = "";
    ..
    ..
    ..

    In another script file, I have the mymethod() function defined like
    this:

    function mymethod()
    {
    ..
    ..
    ..
    }


    I tried various incarnations of setting the onchange event:

    mytextarea.onchange = "mymethod()"; and onchange = "mymethod";. None
    of which works. What's the secret to dynamically defining the
    onchange event?
     
    HugeBob, Jun 3, 2008
    #1
    1. Advertising

  2. HugeBob

    Tom Cole Guest

    On Jun 3, 2:08 pm, HugeBob <> wrote:
    > Hi All,
    >
    > I'm creating textareas via the DOM and I'm trying to add the onchange
    > event to them.  Here's my code:
    >
    > mytextarea = document.createElement("textarea");
    > mytextarea.onchange = mymethod;
    > mytextarea.name = "textarea1";
    > mytextarea.value = "";
    > .
    > .
    > .
    >
    > In another script file, I have the mymethod() function defined like
    > this:
    >
    > function mymethod()
    > {
    > .
    > .
    > .
    >
    > }
    >
    > I tried various incarnations of setting the onchange event:
    >
    > mytextarea.onchange = "mymethod()"; and onchange = "mymethod";.  None
    > of which works.  What's the secret to dynamically defining the
    > onchange event?


    The following worked for me:

    mytextarea.onchange = function() { mymethod(); };
     
    Tom Cole, Jun 3, 2008
    #2
    1. Advertising

  3. HugeBob

    HugeBob Guest

    On Jun 3, 2:36 pm, Tom Cole <> wrote:
    > On Jun 3, 2:08 pm, HugeBob <> wrote:
    >
    >
    >
    > > Hi All,

    >
    > > I'm creating textareas via the DOM and I'm trying to add the onchange
    > > event to them. Here's my code:

    >
    > > mytextarea = document.createElement("textarea");
    > > mytextarea.onchange = mymethod;
    > > mytextarea.name = "textarea1";
    > > mytextarea.value = "";
    > > .
    > > .
    > > .

    >
    > > In another script file, I have the mymethod() function defined like
    > > this:

    >
    > > function mymethod()
    > > {
    > > .
    > > .
    > > .

    >
    > > }

    >
    > > I tried various incarnations of setting the onchange event:

    >
    > > mytextarea.onchange = "mymethod()"; and onchange = "mymethod";. None
    > > of which works. What's the secret to dynamically defining the
    > > onchange event?

    >
    > The following worked for me:
    >
    > mytextarea.onchange = function() { mymethod(); };


    Hi Tom,

    That worked! Thanks.
     
    HugeBob, Jun 3, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jose Suero
    Replies:
    2
    Views:
    9,437
    Jose Suero
    Dec 26, 2003
  2. =?Utf-8?B?TW9ua2V5Qm95?=
    Replies:
    2
    Views:
    450
    =?Utf-8?B?TW9ua2V5Qm95?=
    Jun 6, 2004
  3. Lewis Holmes
    Replies:
    5
    Views:
    2,808
    Allen Chen [MSFT]
    Mar 18, 2009
  4. Bart van Deenen
    Replies:
    3
    Views:
    144
    Bart van Deenen
    Feb 17, 2005
  5. weston
    Replies:
    1
    Views:
    272
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page