0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.