Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Dynamically Assigning OnChange Handler

Reply
Thread Tools

Dynamically Assigning OnChange Handler

 
 
HugeBob
Guest
Posts: n/a
 
      06-03-2008
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?
 
Reply With Quote
 
 
 
 
Tom Cole
Guest
Posts: n/a
 
      06-03-2008
On Jun 3, 2:08*pm, HugeBob <(E-Mail Removed)> 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(); };
 
Reply With Quote
 
 
 
 
HugeBob
Guest
Posts: n/a
 
      06-03-2008
On Jun 3, 2:36 pm, Tom Cole <(E-Mail Removed)> wrote:
> On Jun 3, 2:08 pm, HugeBob <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET Validator is removing client onChange event handler for custom server control Lewis Holmes ASP .Net 5 03-18-2009 07:14 AM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
IE6 problem with onchange handler on dynamically created <select> Bart van Deenen Javascript 3 02-17-2005 03:16 PM
.Net validators broken by client-side script (cloneNode does not copy onchange handler) =?Utf-8?B?TW9ua2V5Qm95?= ASP .Net 2 06-05-2004 11:56 PM
Add Event Handler Dynamically, but assigning function name at run-time, not to static function Jose Suero ASP .Net 2 12-26-2003 08:40 PM



Advertisments