Reputation: 1304
I'm wondering if there's a way to use jquery $.data to bind button1 element to button2 so that when I click button2 I can access the button1 object and extract attributes etc.
The binding code I have is in the following function, this is called by clicking button1 and the this object is passed in as:
button1:
$(".acc-offer-clicked").click(function(e){
e.preventDefault();
getCourierList(this);
});
function getCourierList(obj) {
//Bind the accept button to the choose-courier button
$.data($("#choose_courier"), "accept", obj);
}
later when button1 is clicked I hope to extract button2 as
$("#choose_courier").click(function(e){
e.preventDefault();
var acceptbutton = $(this).data("accept");
});
At this point accept-button is undefined. I've been able to bind {}
type objects before but I've never tried with an actual element. Is this even possible?
Upvotes: 0
Views: 562
Reputation: 8715
I suppose the problem is that
$.data($("#choose_courier"), "accept", obj);
binds data to a newly created jQuery object $("#choose_courier")
. Then, when you try to retrieve it, you create another object, which does not have any data
associated.
Documentation gives the following $.data
signature: jQuery.data( element, key, value )
. Not jQuery object, but element.
For resolve your problem you may use $(selector).data()
.
I've included both variants in this sample Fiddle, check it out.
Upvotes: 1