5

I'm trying to get a div with data-type 'dialog' to display in JQuery Mobile, fired by a Javascript event. The button click in the example below is purely to fire the event.

<head>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            //$.mobile.changePage('#addCatForm');

            $('#createEvent').click (function () {
                console.log('Prove event fired');

                $.mobile.changePage('#addCatForm', {
                    transition: 'pop',
                    changeHash: false,
                    role: 'dialog'
                });
            });

        });
    </script>
</head>
<body>
    <div data-role="page" id="mainPage">
        <button id="createEvent">Create Event</button>
        <div data-role="dialog" id="addCatForm" data-theme="c">
            <p>here is some text</p>
            <div data-role="content">
                <input type="text" id="catText" data-theme="c"/>
                <input data-role="button" data-mini="true" data-theme="b" data-inline="true" type="submit" id="addCat" value="Add Category">

                <button data-role="button" data-mini="true" data-theme="c" data-inline="true" id="canCat">Cancel</button>
            </div>
        </div>
    </div>    
</body>

The console.log output correctly fires, but nothing I can do seems to get the dialog to display.

Any help appreciated.

Thank you,

Gajotres
  • 57,309
  • 16
  • 102
  • 130
JonRed
  • 2,853
  • 5
  • 28
  • 37
  • Put the `dialog` div outside the `data-role=page`. only popups can be inserted inside `data-role=page`. Treat it as a `page` but with `data-role=dialog` instead. – Omar Apr 11 '13 at 10:50

2 Answers2

5

Working example: http://jsfiddle.net/Gajotres/Jx9xM/

$(document).ready(function() {    
    $('#createEvent').click (function () {
        console.log('Prove event fired');

        $.mobile.changePage('#addCatForm', {
            transition: 'pop',
            changeHash: true,
            role: 'dialog'
        });
    });
});

Dialog must be on a same level as a page and not a part of the page. I this case I have moved dialog outside of a page.

JonRed
  • 2,853
  • 5
  • 28
  • 37
Gajotres
  • 57,309
  • 16
  • 102
  • 130
  • Accepting this answer because of the thorough jsfiddle. Thank you both. – JonRed Apr 11 '13 at 11:30
  • 2
    You should not use $(document).ready with jQM, you should use $(document).on("mobileinit", function(){ //apply overrides here }); http://api.jquerymobile.com/global-config/ – Phill Pafford Apr 11 '13 at 12:44
2

Your structure should look like this, data-role=dialog outside data-role=page.

<!-- Page -->
<div data-role="page" id="mainPage">
 <button id="createEvent">Create Event</button>
</div> 
<!-- /Page -->

<!-- Dialog -->
<div data-role="dialog" id="addCatForm" data-theme="c">
 <p>here is some text</p>
 <div data-role="content">
  <input type="text" id="catText" data-theme="c"/>
  <input data-role="button" data-mini="true" data-theme="b" data-inline="true" type="submit" id="addCat" value="Add Category">
  <button data-role="button" data-mini="true" data-theme="c" data-inline="true" id="canCat">Cancel</button>
  </div>
 </div>
 <!-- /Dialog -->
Omar
  • 32,302
  • 9
  • 69
  • 112
  • Great answers to both Omar and @Gajotres. Thank you - it works. However, I've been trying to close this dialog and the $('#addCatForm').dialog('close') function does not seem to behave as I would expect. How do I close this dialog with access to its DOM elements? Thank you. – JonRed Apr 11 '13 at 11:13
  • @JonRed when you close it, it goes back to `#mainpage`? – Omar Apr 11 '13 at 11:18
  • if you want to go back to previous page add `data-rel="back"` attribute to `#addCatForm` div. @JonRed – Omar Apr 11 '13 at 11:22
  • When I run the $('#addCatForm').dialog('close') function, nothing happens at all. – JonRed Apr 11 '13 at 11:23
  • 2
    Take a look at my example again :http://jsfiddle.net/Gajotres/Jx9xM/ I have also changed changeHash: false to changeHash: true. – Gajotres Apr 11 '13 at 11:25
  • 1
    Thanks @Gajotres, I got it at the same time your comment came in. It is in fact the changeHash property that was causing the problem. I'd like to mark both answers as 'Accepted' ;-) – JonRed Apr 11 '13 at 11:28