Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

focusing on textfields 1

Status
Not open for further replies.

MrMiyagi

Technical User
Feb 11, 2009
43
FI
Hi,

I have a function like this to focus on textfields:

function focusEffect(element){
document.getElementById(element).focus();
}

I am using it to an element like this:

onmouseover = "focusEffect('field1')" onMouseout="focusEffect('')


I need to change this so that onmouseover event: "if not allready focused in some other textfield ---> focus on ID="field1".

and for the onMouseout event: "stay focused to the textfield that is currently active/selected"

Thanks for all answers.





 
might not be the best way to do it, but here's one way:

1. When the document is loaded, fire off an event that assigns a new property to all the input boxes called "isInFocus" and set it to false.
Code:
	window.onload = function() {
		var elements = document.getElementsByTagName("input");
		for (var i = 0 ; i < elements.length ; i++) {
			elements[i]["isInFocus"] = false;
		}
	}

2. Create a function that sets this property whenever an input box is in focus or blurred

Code:
	function setFocus(element,blnFocus) {
		element["isInFocus"] = blnFocus;
	}

3. Create a function that checks all the input boxes to see if any are currently in focus

Code:
	function checkFocus() {
		var elements = document.getElementsByTagName("input");
		for (var i = 0 ; i < elements.length ; i++) {
			if (elements[i]["isInFocus"]) {
				return true;
			}		
		}
		return false;
	}

4. Create the onmouseover function to focus an element if no others are in focus

Code:
	function focusElement(element) {
			if (!checkFocus()) {
			element.focus();
			element["isInFocus"] = true;
		}
	}

5. Assign the events to the input boxes add the 'onmouseover' event to the input boxes you want to have this behaviour.
Code:
<input type="text" id="one" onfocus="setFocus(this,true);" onblur="setFocus(this,false);" /><br />
<input type="text" id="two" onfocus="setFocus(this,true);" onblur="setFocus(this,false);" /><br />
<input type="text" id="three" onfocus="setFocus(this,true);" onblur="setFocus(this,false);" /><br />
<input type="text" id="four" [b][COLOR=red]onmouseover="focusElement(this);"[/color][/b] onfocus="setFocus(this,true);" onblur="setFocus(this,false);" /><br />

Thanks to Fotiman of Web Master World - it's surprising what Google can find you...




--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top