3

I want to use jquery dialog to collect user information( user name for example). How do I do that with Jquery and collect data into Javascript variable?

This is my attempt so far:

// Dialog here, how to rewrite this?
$('<form> <input type="text" style="z-index:10000" name="name"> <br> </form>').dialog({modal:true});

// push data to Parse
var Label = Parse.Object.extend("Label");
var result = new Label();
result.set("labels", localStorage.getItem("labels"));
result.set("name", name);

result.save(null, {
  sucess : function(result) {
    alert("Stored data sucessfully!");
  },
  error: function(result, error) {
    alert("Error submitting data, error code:" + error.message);
  }
});

However I couldn't enter text yet.

2 Answers 2

6

You can put any functionality you want in the buttons section so when the user clicks ok, you process their info then. The docs for this are here. For instance:

$('<form><input type="text" style="z-index:10000" name="name"><br></form>').dialog({
  modal: true,
  buttons: {
    'OK': function () {
      var name = $('input[name="name"]').val();
      storeData(name);
      $(this).dialog('close');
    },
    'Cancel': function () {
      $(this).dialog('close');
    }
  }
});

Here is a fiddle that shows this in action (albeit with no styling): http://jsfiddle.net/duffmaster33/zuervqop/1/

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

Comments

-1

The code that processes the dialog should be in its onClose function.

$('<form> <input type="text" style="z-index:10000" class="name"> <br> </form>').dialog({
  modal: true,
  onClose: function() {
    var name = $(this).find(".name").val();
    var Label = Parse.Object.extend("Label");
    var result = new Label();
    result.set("labels", localStorage.getItem("labels"));
    result.set("name", name);

    result.save(null, {
      sucess: function(result) {
        alert("Stored data sucessfully!");
      },
      error: function(result, error) {
        alert("Error submitting data, error code:" + error.message);
      }
    });
  }
});

1 Comment

You will process the results when the user hits cancel if you do it this way

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.