In jQuery you can easily do it like this:
$("#foo").focus(function () {
// Do this.
}).blur(function () {
// Do that.
});
Can we do something similar in JavaScript so we don’t have to repeat #foo in two separate functions?
In jQuery you can easily do it like this:
$("#foo").focus(function () {
// Do this.
}).blur(function () {
// Do that.
});
Can we do something similar in JavaScript so we don’t have to repeat #foo in two separate functions?
Yes, if the function is designed that way.
For example:
function Helper() {
var helper = this;
return function (selector) {
console.log(selector);
return helper;
};
};
var helper = new Helper();
Helper.prototype.blur = function () {
console.log('blur');
return this;
}
Helper.prototype.focus = function () {
console.log('focus');
return this;
}
helper('#foo').focus().blur();
Thanks for the sample code, but it seems that using two separate functions for #foo is easier than a helper.