user1285928
user1285928

Reputation: 1476

Call Jquery dialog and pass a string for message

I want to call JavaScript function and pass a string to it. I tried this way:

function dialog(a){              
    // Dialog           
    $(a).dialog({        
        width: 600,
        buttons: {
            "Ok": function() { 
                $(this).dialog("close"); 
            }, 
            "Cancel": function(event) { 
                $(this).dialog("close");
                event.preventDefault();
            } 
        }
    });

This is the JSF button:

<h:commandButton value="Delete" action="#{SessionsController.deleteSelectedIDs}" 
                    onclick="dialog('Dialog Test')">
    <f:ajax render="@form" execute="@form"></f:ajax>
</h:commandButton>

When I try to pass a string using this way onclick="dialog('Dialog Test')" it's not working. How I can fix this?

Best wishes

Upvotes: 1

Views: 1462

Answers (2)

jfriend00
jfriend00

Reputation: 707376

Passing the string to the dialog() function is likely working just fine. What is probably not working is the resulting:

$(a)

which becomes:

$('Dialog Test')

As that tells jQuery to look for a CSS selector of <Test> objects contained in <Dialog> objects which isn't what you meant at all.

Upvotes: 1

Andrew Whitaker
Andrew Whitaker

Reputation: 126052

You probably want something like this:

function dialog(a) {
    // Dialog           
    $("<div />", { text: a }).dialog({
        width: 600,
        buttons: {
            "Ok": function() {
                $(this).dialog("close");
            },
            "Cancel": function(event) {
                $(this).dialog("close");
                event.preventDefault();
            }
        }
    });
}

Wrap the text in a <div /> that will contain the dialog text before calling dialog on it. Previously you were passing jQuery an invalid selector which is why nothing popped up.

Example: http://jsfiddle.net/4extL/2/

Upvotes: 2

Related Questions