HighDots Forums  

Dynamically Assigning OnChange Handler

Javascript JavaScript language (comp.lang.javascript)


Discuss Dynamically Assigning OnChange Handler in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
HugeBob
 
Posts: n/a

Default Dynamically Assigning OnChange Handler - 06-03-2008 , 01:08 PM






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
  #2  
Old   
Tom Cole
 
Posts: n/a

Default Re: Dynamically Assigning OnChange Handler - 06-03-2008 , 01:36 PM






On Jun 3, 2:08*pm, HugeBob <rnu... (AT) gmail (DOT) com> wrote:
Quote:
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
  #3  
Old   
HugeBob
 
Posts: n/a

Default Re: Dynamically Assigning OnChange Handler - 06-03-2008 , 02:35 PM



On Jun 3, 2:36 pm, Tom Cole <tco... (AT) gmail (DOT) com> wrote:
Quote:
On Jun 3, 2:08 pm, HugeBob <rnu... (AT) gmail (DOT) com> 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
  #4  
Old   
Dan Rumney
 
Posts: n/a

Default Re: Dynamically Assigning OnChange Handler - 06-03-2008 , 02:40 PM



Quote:
The following worked for me:

mytextarea.onchange = function() { mymethod(); };
This should also work

mytextarea.onchange = mymethod


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.