1

I want to open a jQuery UI dialog box and the box will have an input field which will take username. I want that when user presses the OK key on the dialog box, the dialog should return the username back to jQuery somehow

$("#dialog").dialog({
            autoOpen: false,
            width: 600,
            buttons: {
                "Ok": function() {
                $(this).dialog("close");}}
        });
2
  • You can grab the value put into the box using val() as you would any standard input box. Is there something specific you're trying to do with the value? Commented Apr 30, 2012 at 12:47
  • ya i want to use it further in my jquery code, Commented Apr 30, 2012 at 12:49

4 Answers 4

2

A dialog is asynchronous, which makes it meaningless to "return" something.
You can, however, use one of the callback functions to perform actions with the user input.
For example, using the 'OK' button callback:

$("#dialog")
    .append(
        $("<input>")
            .attr({
                "type" : "text",
                "id" : "user-input"
            })
    )
    .dialog({
        autoOpen: false,
        width: 600,
        buttons: {
            "Ok": function() {
                alert( $("#user-input").val() );
                $(this).dialog("close");
            }}
    });
Sign up to request clarification or add additional context in comments.

2 Comments

so when i click ok will i be able to access the $("#user-input").val() variable in the jquery script
yes, because as long as the dialog is open, '#user-input' is part of the DOM.
2

If you just have a div on your page with ID dialog that contains the input field, just select it using a regular jQuery selector when the user presses OK.

Comments

1

Try this on "OK" funcion

var myvalue= $("#yourinputtext").val();
alert(myvalue);

Comments

1

You can capture the value in the ok button click handler, and then do with it as your require:

$("#dialog").dialog({
    autoOpen: false,
    width: 600,
    buttons: {
        "Ok": function() {
            var inputValue = $("#myInput").val();
            // do stuff with the value...

            $(this).dialog("close");}
        }
    }
);

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.