Dynamically Assigning OnChange Handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HugeBob

    Dynamically Assigning OnChange Handler

    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.create Element("textar ea");
    mytextarea.onch ange = mymethod;
    mytextarea.name = "textarea1" ;
    mytextarea.valu e = "";
    ..
    ..
    ..

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

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


    I tried various incarnations of setting the onchange event:

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

    #2
    Re: Dynamically Assigning OnChange Handler

    On Jun 3, 2:08 pm, HugeBob <rnu...@gmail.c omwrote:
    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.create Element("textar ea");
    mytextarea.onch ange = mymethod;
    mytextarea.name = "textarea1" ;
    mytextarea.valu e = "";
    .
    .
    .
    >
    In another script file, I have the mymethod() function defined like
    this:
    >
    function mymethod()
    {
    .
    .
    .
    >
    }
    >
    I tried various incarnations of setting the onchange event:
    >
    mytextarea.onch ange = "mymethod() "; and onchange = "mymethod"; .  None
    of which works.  What's the secret to dynamically defining the
    onchange event?
    The following worked for me:

    mytextarea.onch ange = function() { mymethod(); };

    Comment

    • HugeBob

      #3
      Re: Dynamically Assigning OnChange Handler

      On Jun 3, 2:36 pm, Tom Cole <tco...@gmail.c omwrote:
      On Jun 3, 2:08 pm, HugeBob <rnu...@gmail.c omwrote:
      >
      >
      >
      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.create Element("textar ea");
      mytextarea.onch ange = mymethod;
      mytextarea.name = "textarea1" ;
      mytextarea.valu e = "";
      .
      .
      .
      >
      In another script file, I have the mymethod() function defined like
      this:
      >
      function mymethod()
      {
      .
      .
      .
      >
      }
      >
      I tried various incarnations of setting the onchange event:
      >
      mytextarea.onch ange = "mymethod() "; and onchange = "mymethod"; . None
      of which works. What's the secret to dynamically defining the
      onchange event?
      >
      The following worked for me:
      >
      mytextarea.onch ange = function() { mymethod(); };
      Hi Tom,

      That worked! Thanks.

      Comment

      • Dan Rumney

        #4
        Re: Dynamically Assigning OnChange Handler

        The following worked for me:
        >
        mytextarea.onch ange = function() { mymethod(); };
        This should also work

        mytextarea.onch ange = mymethod

        Comment

        Working...