Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts

    shorten form focus script

    Can antone tell me how to shorten this script? The script is used to highlight which input/textarea box has focus in IE.

    sfFocus = function() {
    var sfEls = document.getElementsByTagName("INPUT");
    for (var i=0; i<sfEls.length; i++) {
    sfEls[i].onfocus=function() {
    this.className+=" sffocus";
    }
    sfEls[i].onblur=function() {
    this.className=this.className.replace(new RegExp(" sffocus\\b"), "");
    }
    }
    var sfEl = document.getElementsByTagName("TEXTAREA");
    for (var i=0; i<sfEl.length; i++) {
    sfEl[i].onfocus=function() {
    this.className+=" sffocus";
    }
    sfEl[i].onblur=function() {
    this.className=this.className.replace(new RegExp(" sffocus\\b"), "");
    }
    }
    }
    if (window.attachEvent) window.attachEvent("onload", sfFocus);

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,513
    Thanks
    3
    Thanked 504 Times in 491 Posts
    Code:
    sfFocus = function(){
     var sfEls=document.getElementsByTagName('*');
     for (var i=0;i<sfEls.length;i++){
      if (sfEls[i].tagName=='INPUT'||sfEls[i].tagName=='INPUT'){
       sfEls[i].onfocus=function(){ this.className+=' sffocus'; }
       sfEls[i].onblur=function(){ this.className=this.className.replace(' sffocus',''); }
      }
     }
    }
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Tried out that script but it didn't work. Also did a few variations on it and it didn't work either.

    Tried this to no avail...
    var sfEls = document.getElementsByTagName("input") = document.getElementsByTagName("textarea");

    Best
    CB


  •  

    Posting Permissions

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