views:

45

answers:

3

Hi

I have object created using jquery where each object has it's own binding.

function closeButton(oAny){

var div = create_div();
$(div).attr("id","btn_"+$(oAny).attr("id"));
var my_parent = this;

$(div).html("<img src='"+ my_parent._base_url +"/assets/images/close.gif'>");
$(div).click(function(){
       alert("do some action here");
});

return div;

}


var MyObject = WindowObject();
var btn = closeButton(MyObject);

$(myobject).append(btn);
$("body").append(myobject); //at this point button will work as i expected    


//save to array for future use
ObjectCollections[0] = myobject;

//remove
$(myobject).remove();

$(body).append(ObjectCollections[0]); // at this point button will not work

For the first time i can show my object and close button is working as i expected. But if i save myobject to any variable for future use. It will loose its binding. Anybody ever try to do this ? Is there any work around ? or It is definitely a bad idea ? .And thanks for answering my question.

+1  A: 

instead of click binding like this you need to use live function which keeps the events alive

$('yourelement').live('click', function(){
// some stuff
});
XGreen
Ah, you beat me by seconds :P
fudgey
I have try your suggestion. But still not work. and I have my own answer now. I will use static binding a.k.a onClick.this is what I do.$(btn).attr("onClick","someFunction('#"+ $(myobject).attr("id")+"')");thanks for your answer XGreen and Fudgey.
Ahmad Satiri
A: 

Have you tried using jQuery live?

It basically allows you to dynamically add objects and have the bindings preset for them. Try something like

$(div).live('click', function(){
 alert("do some action here");
});
fudgey
A: 

I have try XGreen and Fudgey suggestion to use Live binding.

But still won't work. I have my own answer now. I will use static binding a.k.a onClick.

this is what I do.

$(btn).attr("onClick","someFunction('#"+ $(myParentObject).attr("id")+"')");

thanks for your answer XGreen and Fudgey.

Ahmad Satiri