var hints = (function () {
/**
* @var Default selector to display hints for
*/
var defaultSelector = 'input[type=text]',
/**
* This object contains all the public methods.
*/
handlers = {
/**
* Apply fn on the hint-element belonging to the context element
*/
action: function (fn) {
var hint = $(this).parent().find('.hint');
if(hint) {
hint[fn]();
}
},
/**
* Show the hint of the element that received focus
*/
focusListener: function () {
handlers.action.call(this, 'show');
},
/**
* Hide the hint of the element that lost focus
*/
blurListener: function () {
handlers.action.call(this, 'hide');
},
/**
* Initialize listeners for the elements matched by selector
* @param string [specificSelector] Override the default selector
* with another one
*/
init: function (specificSelector) {
var selector = specificSelector || defaultSelector;
$(selector).focus(handlers.focusListener);
$(selector).blur(handlers.blurListener);
}
};
return handlers;
}());
Этот объект можно назвать так, чтобы добавить функциональность подсказки:
hints.init(); // Add focus and blur listeners on all input-text elements
// Add focus and blur listeners on all input-text and radio elements
hints.init('input[type=text], input[type=radio]');