Call div inside a function

I'm trying to create a popup similar to an iOS alert view, with jQuery mobile 1.4.3. I need my warning messages to be triggered from javascript events, like a confirmation message with a OK button showing the ajax response call to the web service.

My first problem is passing data thru a popup widget, with much searching I fount it impossible.

I found a peace of code that is almost what I need:

$('#index').live('pagebeforeshow',function(e,data){   
    $("#test-button").bind('click',function(event, ui){
        $('<div>').simpledialog2({
            mode: 'button', 
            headerText:  'Dialog', 
            buttonPrompt: 'Please, don\'t ruin my blue world', 
            buttons : {'close': {click: function() {}}},
            width: '120px'
        })
    });  
});

The problem is that I need this function to be called not from a button click but by a function: Something like this:

//POPUP CODE
// 1 == warning green

// 2 == warning yellow

// 3 == warning red

function customAlert(message, typeOfWarning){

    if(typeOfWarning == "1"){
        var auxStr = "green";
    };
    if(typeOfWarning == "2"){
        var auxStr = "yellow";
    };
    if(typeOfWarning == "3"){
        var auxStr = "red";
    };


    $('<div>').simpledialog2({
        mode: 'button', 
        headerText:  'Dialog', 
        buttonPrompt: 'Please, don\'t ruin my blue world', 
        buttons : {'close': {click: function() {}}},
        width: '120px'
    })
};

I'm new to JavaScript and jQuery Mobile, need help, can't make it work.

Thanks in Advance.

Answers:

Answer

jQuery Mobile's Popup widget has many ways to manipulate it. It can be called by a button or opened programmatically. The structure is simple, however, note that only page div should be the direct parent of a popup.

<div data-role="page">
  <div data-role="popup" id="foo">
    <!-- content -->
  </div>
</div>

To open it statically by a button or an anchor:

<a href="#foo" data-rel="popup" data-transition="pop">Popup</a>

To open it programmatically:

$("#foo").popup("open");

Also, you can use special events for any purpose you want, e.g. popupafteropen and popupafterclose.

The below is an example of a dynamically created popup.

// close button
var closeBtn = $('<a href="#" data-rel="back" class="ui-btn-right ui-btn ui-btn-b ui-corner-all ui-btn-icon-notext ui-icon-delete ui-shadow">Close</a>');

// text you get from Ajax
var content = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing. Morbi convallis sem et dui sollicitudin tincidunt.</p>";

// Popup body - set width is optional - append button and Ajax msg
var popup = $("<div/>", {
    "data-role": "popup"
}).css({
    width: $(window).width() / 1.5 + "px",
    padding: 5 + "px"
}).append(closeBtn).append(content);

// Append it to active page
$.mobile.pageContainer.pagecontainer("getActivePage").append(popup);

// Create it and add listener to delete it once it's closed
// open it
$("[data-role=popup]").on("popupafterclose", function () {
    $(this).remove();
}).on("popupafteropen", function () {
    $(this).popup("reposition", {
        "positionTo": "window"
        /* custom position
        x: 150, 
        y: 200 */
    });
}).popup({
    dismissible: false,
    history: false,
    theme: "b",
    /* or a */
    overlayTheme: "b",
    /* or a */
    transition: "pop"
}).popup("open");

Demo

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.