0

This should be fairly easy but I can't get my head around it. I have googled but can't find answers. I want to be able to capture some data when button A is clicked and submit those data when button B is clicked, via AJAX.

I have been thinking of this:

$(document).on('click', 'button-a', function(){
    var captureData = $('available-data').text();

    $(document).on('click', 'button-b', function(){
        submitData(captureData);
    })
})

How can I achieve this?

4
  • Check your selectors! Commented Dec 13, 2014 at 18:04
  • @Wilmer: I don't get you. Which of the selector? Commented Dec 13, 2014 at 19:40
  • All of them, check the jquery docs. Commented Dec 13, 2014 at 23:02
  • May be the best way to check about it is to set var captureData = undefined and see that undefined is passed or not. Commented Dec 14, 2014 at 1:50

1 Answer 1

2

Declare captureData globally. When you click on button-a the capture the value to captureData and when you click on button-b then submit it. You should not include button b click event inside button-a click event.

var captureData = 'default value';
$(document).on('click', 'button-a', function(){
    captureData = $('available-data').text();
});
$(document).on('click', 'button-b', function(){
    submitData(captureData);
});
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.