SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2008
    Location
    Oklahoma City
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with attachEvent

    I'm trying to set an event handler and avoid using the event parameter in my input tags. I'll be addressing the cross-browser issue with this function but I hit a wall implementing the IE code.

    I have this strategy in use for another event where the function does not need a parameter and it works great, such as: oInputs[i].attachEvent('onkeyup',formhasChanged);

    However when I need to pass a value to my event function I get an "object required" error when the function calls the input/textarea object on the line "var y=document.getElementById(elem).value;". The object reference elem is apparently not an object.

    Any ideas on how to accomplish this?
    Thanks-Lyndon

    Code:
    <body onload="setonkeyupEvent();">
    
    function setonkeyupEvent(){
     var oInputs = document.getElementsByTagName('textarea');
     for (var i = 0; i < oInputs.length; i++){ oInputs[i].attachEvent('onkeyup',function(){upperCase(this.id)})
      }
    }
    
    function upperCase(elem){
     var y=document.getElementById(elem).value;
     document.getElementById(elem).value=y.toUpperCase();
    }
    Last edited by LyndonOHRC; Jul 24, 2008 at 12:21.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The code is passing the identifier of the textarea, so if the textarea doesn't have an id attribute then it's not going to work. The solution is to pass the element instead.

    Code javascript:
    function setOnkeyupEvent() {
        var els = document.getElementsByTagName('textarea'),
            elsLen = els.length;
        var i;
        for (i = 0; i < elsLen; i += 1) {
    		els[i].onkeyup = function() {
    			upperCase(this);
    		};
    	}
    }
     
    function upperCase(el){
        el.value=el.value.toUpperCase();
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Jul 2008
    Location
    Oklahoma City
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That did it.
    Lyndon


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •