UnderZ

Javascript Library.

Bind or trigger a focus event on an element.

Execute a function when the form field gets focus. or trigger focus event for an element.


Bind focus event

_z(selector).focus(function);

Returns: Object _z(selector)

This method is a shortcut for _z(selector).on( “focus”, function).

To unbind focus event: Unbind focus event


Trigger focus event

_z(selector).focus();

Returns: Object _z(selector)


Unbind focus event

_z(selector).un("focus", [function]);

Returns: Object _z(selector)


Examples

<input type="text" id="text" value="Show Message & Change Title">
<input type="text" value="Change Title" class="btnCTitle">
<input type="text" value="Unbind 'Change Title' event" class="btnUBEvent">

<script>
// bind focus event by element id
_z("#text").focus(function () { 
	alert("Focus in!");
});

// bind focus event by element tag - this will bind the event with all elements with "input" tag.
_z("text").focus(function () { 
	// set title as the element value
	document.title = _z(this).val();
});

// unbind focus event by element class.
_z(".btnUBEvent").click(function () {
	_z(".btnCTitle").un("focus");
});


// trigger focus event
_z(".btnCTitle").focus(); // change document title to 'Change Title'
_z("#button").focus(); // alert 'Focus in!' and change document title to 'Show Message & Change Title'
_z(".btnUBEvent").click(); // change document title to 'Unbind "Change Title" event' and unbind focus event on .btnCTitle button
</script>

Recommended: bind events in .ready() to execute the event when the document is fully loaded.