Greg Amer

Event Handlers

onFocus

e-4


JavaScript:
gascripts.com onFocus event handlers execute script when the cursor focuses on an object.

 

Hard Reload
To See Menu

 

<script language="JavaScript">
<!--
onFocus
function junk_(form){

var write_="This is only a test, but it could easily be real"
form.one.value = write_}
// -->
</script>
This function simply writes a string to the textarea object one, upon being called by the onFocus event handler declared in its tag.      
<form>
<textarea rows="3" name="one" cols="20" onFocus="junk_(this.form)"></textarea>
<input type="reset" value="Reset">
</form>
onFocus calls the function junk_. This event handler refers to the state of the cursor or mouse.

Not to be confused with the
focus() method which actually forces focus on an object.



method

focus()

fill in this form and click focus ()

<script language="JavaScript">
<!--

function testfocus(form){
var isGood = document.two.one.value

if ( isGood != "bingo" ) {
alert( "please type in 'bingo'")

document.two.one.value = ""
document.two.one.focus() }}

// -->
</script>
This script uses the focus() method to return the user to an improperly completed form field.

The form name is two, and contains the text elements one & two. By testing the user input against a predifined string "bingo", incorrect entries are returned to the incorrect field.

This event works with select, text, text area objects & window objects.

 


[back]   [home]   [next]