Dynamically Assigning OnChange Handler

H

HugeBob

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?
 
T

Tom Cole

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(); };
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top