Click here to Skip to main content
Licence CPOL
First Posted 20 Aug 2006
Views 100,031
Downloads 317
Bookmarked 9 times

Change the background color of the currently focused element

By | 31 Aug 2006 | Article
How to change the background color of the currently focused element when the user tabs off.

Sample Image - focusBGColor.jpg

Introduction

When the user tabs off from an element, browsers by default just draw a border around the focused element. Sometimes it is hard to notice and if we want to help the user to find this easily, we can change the background color of the currently focused element. It will help them easily identify the focused element.

How do we do this?

We need some JavaScript to set and reset the background color.

var bkColor = "red";
function getEvent(e){
  if(window.event != null) {
    return event;
  }
  return e;
}
function setBKColor(e){
 e = getEvent(e);
 var src =  e.srcElement || e.target;
 if(src != null) {
   src.style.bkColor = src.style.backgroundColor;
   src.style.backgroundColor = bkColor;
 }
}
function reSetBKColor(e){
 e = getEvent(e);
 var src =  e.srcElement || e.target;
 if(src != null) {
   src.style.backgroundColor = src.style.bkColor;
 }
}

You can set any color for the global variable bkColor which will be used for setting the background color. You can use an RGB color code or a color name such as red, blue, green, etc. I have assigned red as my focus background color.

setBKColor will be assigned to the onFocus event. Whenever the element receives focus, the browser will call this method. In that method, we store the original bkColor in a temp variable and we just set the global bkColor variable to the current element's background color.

reSetBKColor will be assigned to the onBlur event. Whenever the element loses focus, the browser will call this method. In that method, we just set the temp bkColor to the current element's background color, and it will reset to the original background color.

We can directly assign those JavaScripts to every HTML form element tag or write a global event handler to attach the event to all form elements.

I will show you both ways.

Adding to the HTML tag:

<input type="text" onfocus="setBkColor(event);" onblur="reSetBKColor(event);"
<input type="radio" id= "rd1" onfocus="setBkColor(event);" onblur="reSetBKColor(event);"

If you like to add a global handler:

<script language="javascript">
function attachEvent(name,element,callBack) {
    if (element.addEventListener) {
      element.addEventListener(name, callBack,false);
    } else if (element.attachEvent) {
      element.attachEvent('on' + name, callBack);
    }
}

function setListner(eve,func) {
   var ele = document.forms[0].elements;
   for(var i = 0; i < ele.length;i++) {
    element = ele[i];
    if (element.type) {
      switch (element.type) {
        case 'checkbox':
        case 'radio':
        case 'password':
        case 'text':
        case 'textarea':
        case 'select-one':
        case 'select-multiple':
           attachEvent(eve,element,func);
       }
     }
  }
}
setListner("focus",setBKColor);
setListner("blur",reSetBKColor);
</script>

We need to call the setListner function for the focus and blur functions. It will loop through all elements and attach the particular function (second argument) to the event (first argument).

Now if you launch the HTML and tab off from one field to another or click inside a field, you will see the previous element's bkColor reset to the original bkColor and the focused element will change to the new bkColor.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

senthil karuppaiah

Web Developer

United States United States

Member

Fourteen years of progressive experience in Software Product Development, tactical planning, project and client management, demonstrated success in leadership, critical thinking, problem solving and analysis. Diverse knowledge and experience with multiple development methodologies, reengineering, software engineering, and integration of heterogeneous systems.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionjava files in a folder outside ... Pinmemberwalterix6:29 29 Feb '12  
Questionbackground color change to the disabled and readOnly fields PinmemberSpace_b18:45 17 Sep '10  
Generalit doesnt work with UpdatePanel PinmemberSelcuk OZDOGAN3:57 18 Feb '09  
Questiondoes it work without a form tag? PinmemberAlderaic-3:16 5 Sep '06  
QuestionOnly for IE ? Pinmemberstevejebson21:57 29 Aug '06  
AnswerRe: Only for IE ? Pinmembersenthil karuppaiah6:17 31 Aug '06  
GeneralNice one [modified] PinmemberRamesh Ramalingam19:38 20 Aug '06  
GeneralRe: Nice one Pinmembersenthil karuppaiah4:16 21 Aug '06  
GeneralRe: Nice one PinmemberRamesh Ramalingam18:20 21 Aug '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120528.1 | Last Updated 31 Aug 2006
Article Copyright 2006 by senthil karuppaiah
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid