3

I'm a begginer jQuery dev, and I'm using the jQuery UI dialog to show up properties of an object (whatever).

var $dialog = $('<div></div>')
    .html('This dialog will show every time!')
    .dialog({
        autoOpen: false,
        title: 'Properties'
    });

So this is the dialog, and let's say I have a FOR structure in which I add properties (p tags) and for some of those properties I want a button.

var dialogHtml = "";
var dialog_buttons = {};
for (var key in d.properties){
    var str;
    var str = '<p>' + something + '</p>';

    if (condition){
        dialog_buttons[key] = function()
            { functionName(key); };
    }

    dialogHtml = dialogHtml + str;
}
$dialog.dialog( "option", "buttons", dialog_buttons );
$dialog.dialog('open');

And somewhere else I have the function:

function functionName(key){
    // something something
}

This is where I have the problem: the key variable that's passed to the function... when the button is called the key is the last value from the iteration. Let's say we have keys 1, 2, 3, 4 then when the button is clicked, the key parameter will be 4.

I want when I click a button from the dialog, to know which button was pressed.

Can anybody help me?

Thanks!

1
  • it will be useful if you could post the answer you found out. Commented Sep 20, 2013 at 6:23

1 Answer 1

3

Using the event data from the click event (see here)

$('#btn').click(function(e) {
  functionName(e.target);
});

Explanation of Event.Target

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

1 Comment

Thanks for that! I found by my own the answer, but didn't knew exactly the reason. Thanks again!

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.