addEventListener, onkeyup, textarea

  • Thread starter kenzie.campbell
  • Start date
K

kenzie.campbell

I have a small onkeyup script for a textarea, and I'd like to move it
out to a separate file with the addEvent function below. Can someone
demonstrate this for me? (My attempts have been unsuccesful.)


<textarea name="text_content" id="text_content" cols="40" rows="15"
onkeyup="textLimit()></textarea>


function addEvent(obj, evType, fn) {
if (obj.addEventListener){
obj.addEventListener(evType, fn, true);
return true;
}
else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
}
else {
return false;
}
}


Thanks!
 
M

Martin Honnen

I have a small onkeyup script for a textarea, and I'd like to move it
out to a separate file with the addEvent function below. Can someone
demonstrate this for me? (My attempts have been unsuccesful.)


<textarea name="text_content" id="text_content" cols="40" rows="15"
onkeyup="textLimit()></textarea>


function addEvent(obj, evType, fn) {
if (obj.addEventListener){
obj.addEventListener(evType, fn, true);
return true;
}
else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
}
else {
return false;
}
}

That addEvent function takes the object to add the event
handler/listener to as the first argument so you could use
var obj = document.getElementById('text_content');
to get at the textarea object, then you need the second argument, the
event type e.g. 'keyup', and the third arugment, the event
handler/listener, a function object taking an event argument e.g.
function textLimit (evt) {
evt = evt || window.event;
// deal with evt here
}
and then you need to call the function e.g.
addEvent(obj, 'keyup', textLimit);
you need to do this after the <textarea> has been parsed.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top