Search code examples
javascriptthisgetelementsbytagnameattachevent

attachEvent can't use THIS javascript


I require Cross compatability IE7 upwards For an event listener. Due to the system I am using I can only use an external js file to create the listeners. I wish to create a rule for all textarea boxes, making they increase in height when they wrap the text meaning that you never have to scroll (They need to be printed with all the text showing). This works fine using addEventListener, but because I can't uses THIS I can not get this to work generically using attachEvent.

<html>
<style>
input[type="text"], textarea{ width:10%;}
</style>
<script>
function gettext(){
var x=document.getElementsByTagName("textarea");
var ent; 
for(i = 0; i < x.length; i++){
if (x.item(i).addEventListener) {
    x.item(i).addEventListener("keyup", function () {ChangeDepth(this)}, false)
    x.item(i).addEventListener("mouseup", function () {ChangeDepth(this)}, false)
    x.item(i).addEventListener("change", function () {ChangeDepth(this)}, false)
 }            
 else {
   ent = document.getElementById(x.item(i).id)
   x.item(i).attachEvent ("onkeyup", function () {ChangeDepth(ent)});
   x.item(i).attachEvent ("onmouseup", function () {ChangeDepth(ent)});
   x.item(i).attachEvent ("onchange", function () {ChangeDepth(ent)});

  }
}
}

function ChangeDepth(e){
var t = e.scrollHeight +'px';
if ((e.clientHeight < e.scrollHeight)||(e.clientHeight > e.scrollHeight)){e.style.height = t ;}
}
</script>
<body onload="gettext()">
<p>(Type in the textarea box when the text wraps it will increase the height of the box)<br />
<textarea name="c" cols="" rows="1" id="c"  ></textarea>
<br />
<textarea name="f" cols="" rows="1" id="f"  ></textarea></p>
</body>
</html>

This only works for the last textarea field in IE 8. I am assuming this because It only has the value of the last object. No idea what to do next


Solution

  • http://jsfiddle.net/KAy9a/

    function wireUp() {
        console.log('wiring');
        var x = document.getElementsByTagName("textarea");
        var ent;
        for (i = 0; i < x.length; i++) {
            if (x.item(i).addEventListener) {
                x.item(i).addEventListener("keyup", ChangeDepth, false)
                x.item(i).addEventListener("mouseup", ChangeDepth, false)
                x.item(i).addEventListener("change", ChangeDepth, false)
            }
            else {
                x.item(i).attachEvent("onkeyup", ChangeDepth);
                x.item(i).attachEvent("onmouseup", ChangeDepth);
                x.item(i).attachEvent("onchange", ChangeDepth);
    
            }
        }
    }
    
    function ChangeDepth(e) {
        // instead of passing in the element, we get the Event object
        // we can then get the event target (or srcElement) to know where the event came from    
        e = e || event;
        e = e.target || e.srcElement;
    
        // the code below is the same you had before:
        var t = e.scrollHeight + 'px';
        if ((e.clientHeight < e.scrollHeight) || (e.clientHeight > e.scrollHeight)) {
            e.style.height = t;
        }
    }