GVillani82
GVillani82

Reputation: 17429

jquery: passing parameter to modal

I have the following in a table, repeated for each row:

<a <?php echo 'id="'.$id.'"'; ?> class="custom-dialog-btn btn btn-small">
   <i class="icon-trash"></i>
</a>

where id is different for each line, because it is taken from a table in database where it is the primary key.

Then, I used th following jQuery code:

$(".custom-dialog-btn").bind("click", function (event) {
                $("#mws-jui-dialog").dialog("option", {
                    modal: false
                }).dialog("open");
                event.preventDefault();
            });

      $("#mws-jui-dialog").dialog({
        autoOpen: false,
        title: "Alert",
        modal: true,
        width: "640",
        buttons: [{
            text: "NO",
        id: "cancel_button",
            click: function () {
                $(this).dialog("close");
            }
        },
     {
        text: "OK",
        id: "confirm_button",
            click: function () {
                myremovefuntion(id); // I need the id
        }}
    ]
    });

that refers to the dialog:

<div id="mws-jui-dialog">
    <div class="mws-dialog-inner">
    <h2>Are you sure you want to delete?</h2>
    </div>
</div>

How can I pass the id to the modal?

Upvotes: 1

Views: 1470

Answers (3)

Code Lღver
Code Lღver

Reputation: 15603

Use the following code to get the id:

var id = $(this).prop("id");

And you will get the id of that which element you have clicked.

Upvotes: 0

depoulo
depoulo

Reputation: 345

While Emin's answer certainly works, I wonder if this fabuolus jQuery UI library perhaps has a dialogue flavour analogue to JavaScript's native confirm() method? Or perhaps a possibility to pass in callback functions? This would remove the need from the dialogue code containing business logic.

Upvotes: 0

Subliminal Hash
Subliminal Hash

Reputation: 13744

You can append a data attribute to your dialog div as follows;

$('a').click(function (e) {
    e.preventDefault();
    $("#your_dialog").data('mycustomdata', $(this).prop('id')).dialog('open');
});

And retreive it like this

    $("#your_dialog").dialog({
        autoOpen: false,
        resizable: false,
        height:200,
        modal: true,
        buttons: {
            Cancel: function() {
                $(this).dialog('close');
            },
            'Delete': function() {
                $(this).dialog('close');
                var mydata = $(this).data('mycustomdata'); // = gives you the id of anchor element
// some delete logic
            }
        }
    });

Upvotes: 3

Related Questions