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. Advertisements

  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. Advertisements

  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. Advertisements

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,815
    Jose Suero
    Dec 26, 2003
  2. =?Utf-8?B?TW9ua2V5Qm95?=
    Replies:
    2
    Views:
    602
    =?Utf-8?B?TW9ua2V5Qm95?=
    Jun 6, 2004
  3. Lewis Holmes
    Replies:
    5
    Views:
    3,162
    Allen Chen [MSFT]
    Mar 18, 2009
  4. Bill
    Replies:
    1
    Views:
    261
    Thomas 'PointedEars' Lahn
    Oct 23, 2003
  5. Bart van Deenen
    Replies:
    3
    Views:
    226
    Bart van Deenen
    Feb 17, 2005
  6. aundro
    Replies:
    13
    Views:
    379
    aundro
    Nov 4, 2005
  7. David McNerney
    Replies:
    4
    Views:
    395
    David McNerney
    Apr 7, 2006
  8. weston
    Replies:
    1
    Views:
    480
    Richard Cornford
    Sep 22, 2006
Loading...