10

I want to open a jQuery Mobile popup programmatically and then close it after some seconds, here is my code :

is there anything wrong, cause I'm not getting what I want to show

$( "#p" ).popup( "open" ); 
setTimeout( function(){ $( "#p" ).popup( "close" ); }, 5000 );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>

<div data-role="popup" id="p" data-position-to="window" data-transition="turn"><p>This is a completely basic popup, no options set.</p></div>

JSFIDDLE DEMO

pstanton
  • 35,033
  • 24
  • 126
  • 168
Yasser B.
  • 835
  • 6
  • 20
  • 34

2 Answers2

15

Make sure your code is within a page event handler like pagecreate so that jQM has initialized the popup widget:

<div data-role="page" id="page1">
     <div data-role="header">
        <h1>My page</h1> 
    </div>  
    <div role="main" class="ui-content">
        <button id="btnpopup">Show Dynamic Popup</button>
    </div> 

    <div data-role="popup" id="p" data-position-to="window" data-transition="turn"><p>This is a completely basic popup, no options set.</p></div>
</div>  

$(document).on("pagecreate","#page1", function(){ 
    $("#btnpopup").on("click", function(){
        $("#p").popup("open"); 
        setTimeout(function(){  $("#p").popup("close"); }, 5000);
    });
});

Working DEMO

ezanker
  • 24,628
  • 1
  • 20
  • 35
8

it is actually simple:

 $("#chatWindow").popup("open");
Amir
  • 1,667
  • 3
  • 23
  • 42