Reputation: 10659
I am trying to do some things on a form element blur. The problem I'm having is passing the element's info such as ID, class, etc to a second function. I've simplified it down for this example:
function otherfunction() {
var inputID = $(this).attr("id");
alert(inputID);
}
$(".formelement").blur(function () {
// Do some stuff here
otherfunction();
});
Of course, the alert box says that the inputID is undefined. How can I get the element's info passed to the otherfunction?
Upvotes: 3
Views: 1548
Reputation: 35417
Pass the input as an argument:
function otherfunction(el) {
var inputID = $(el).attr("id");
alert(inputID);
}
$(".formelement").blur(function () {
// Do some stuff here
otherfunction(this);
});
Or, use Function.prototype.apply
:
function otherfunction() {
var inputID = $(this).attr("id");
alert(inputID);
}
$(".formelement").blur(function () {
// Do some stuff here
otherfunction.apply(this);
});
Upvotes: 3
Reputation: 11683
Use $.proxy in case of:
$(".formelement").blur($.proxy(otherfunction, this));
And otherwise javascript's call or apply:
$(".formelement").blur(function () {
// Do some stuff here
otherfunction.call(this); // or otherfunction.apply(this);
});
Upvotes: 2
Reputation: 4497
I think u can use like this :
function otherfunction(obj) {
var inputID = $(obj).attr("id");
alert(inputID); }
$(".formelement").blur(function () {
otherfunction($(this));
});
Upvotes: 0