addEventListener, onkeyup, textarea

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kenzie.campbell@gmail.com

    #1

    addEventListener, onkeyup, textarea

    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_cont ent" id="text_conten t" cols="40" rows="15"
    onkeyup="textLi mit()></textarea>


    function addEvent(obj, evType, fn) {
    if (obj.addEventLi stener){
    obj.addEventLis tener(evType, fn, true);
    return true;
    }
    else if (obj.attachEven t){
    var r = obj.attachEvent ("on"+evType , fn);
    return r;
    }
    else {
    return false;
    }
    }


    Thanks!

  • Martin Honnen

    #2
    Re: addEventListene r, onkeyup, textarea



    kenzie.campbell @gmail.com wrote:
    [color=blue]
    > 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_cont ent" id="text_conten t" cols="40" rows="15"
    > onkeyup="textLi mit()></textarea>
    >
    >
    > function addEvent(obj, evType, fn) {
    > if (obj.addEventLi stener){
    > obj.addEventLis tener(evType, fn, true);
    > return true;
    > }
    > else if (obj.attachEven t){
    > var r = obj.attachEvent ("on"+evType , fn);
    > return r;
    > }
    > else {
    > return false;
    > }
    > }[/color]

    That addEvent function takes the object to add the event
    handler/listener to as the first argument so you could use
    var obj = document.getEle mentById('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.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    Working...