In the code below i have util.doSomething()
method which takes json object as parameter. When util is done doing something it calls onDone
event handler by passing a response
as parameter.
I wanted to know in the code below if it is possible to pass id
to update
event handler?
$(function(){
$('#btn').click(function(){
var id = $(this).attr('id');
util.doSomething({
property1: "SomeValue1",
property2: "SomeValue2",
onDone: update //ho do i pass id to update event handler?
})
})
function update(response,id)
{
//update
}
})
I know i can get hold of id using inline event handler. like
$("#btn").click(function(){
var id = $(this).attr('id');
util.doSomething({
property1: "SomeValue1",
property2: "SomeValue2",
onDone: function(response){
// now i can use id here
}
})
})
Instead of setting onDone
to update
, you can set it to a function that calls update
with the arguments you want.
util.doSomething({
property1: "SomeValue1",
property2: "SomeValue2",
onDone: function(response) {
return update(response, id);
}
})
You can use the .bind method and the arguments object inside the function to access extra parameters you want to pass in
$(function(){
$('#btn').click(function(){
var id = $(this).attr('id');
util.doSomething({
property1: "SomeValue1",
property2: "SomeValue2",
onDone: update.bind(this, id)
})
})
function update()
{
console.log(arguments); // inside arguments you should see all your parameters
// arguments[0] your id
// arguments[1] your response
}
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.