jQuery Mobile get button that opened Popup

jquery, jquery-mobile

Solution

Adding a click handler to the button seems to work. In this handler, modify the popup before it gets shown:

$('a[data-rel="popup"]').click(function () {
    var link = $(this);
    var data = link.attr('customAttr')
    var popup = $(link.attr('href')); // assume href attr has form "#id"
    popup.append(($('<p />').text(data)));
});

This is a generic handler which supports a page with multiple buttons/popups. If some buttons should not have this behaviour, I would add a class to the desired button, and make the `a[data-rel="popup"]` selector more specific.

See fiddle: http://jsfiddle.net/cPRCU/3/

Problem

I have a listview, when I click a link in the listview it launches a popup. For simplification purposes I have omitted the listview and am starting with just a single button. I want to retrieve attributes from the button that caused the popup to show, in my example the attribute named `customAttr`. I then want to insert the value into `popupBasic`. Here is my very basic sample jQuery Mobile code: ``` <a href="#popupBasic" data-rel="popup" customAttr="value">Basic Popup</a> <div data-role="popup" id="popupBasic"> <p>This is a completely basic popup, no options set.</p> </div> ``` jsFiddle: http://jsfiddle.net/cPRCU/2/ Normally when I work with jQuery (non-Mobile) I am more involved with the click event/opening of popup's/dialogs. I would like to be able to read the button that caused the popup to show, how can I do this?

Original source