Alex Rivera | Logout

Display Messages with jQuery UI

Asked 2011-03-11T00:18:36.167
16

I saw these on jQuery UI's themes page, and thought they were neat.

I wonder how to display messages with these styles both in a static way and with JavaScript.

Thanks in advance.

Edit
Report

1 Answer

2

I have adapted a short jQuery function to convert a given set of divs (containing text) into error/highlight elements.

You can see it in action on this jsFiddle.

Here is the javascript:

//function to create error and alert dialogs
function errorHighlight(e, type, icon) {
    if (!icon) {
        if (type === 'highlight') {
            icon = 'ui-icon-info';
        } else {
            icon = 'ui-icon-alert';
        }
    }
    return e.each(function() {
        $(this).addClass('ui-widget');
        var alertHtml = '<div class="ui-state-' + type + ' ui-corner-all" style="padding:0 .7em;">';
        alertHtml += '<p>';
        alertHtml += '<span class="ui-icon ' + icon + '" style="float:left;margin-right: .3em;"></span>';
        alertHtml += $(this).text();
        alertHtml += '</p>';
        alertHtml += '</div>';

        $(this).html(alertHtml);
    });
}

//error dialog
(function($) {
    $.fn.error = function() {
        errorHighlight(this, 'error');
    };
})(jQuery);

//highlight (alert) dialog
(function($) {
    $.fn.highlight = function() {
        errorHighlight(this, 'highlight');
    };
})(jQuery);
answered 2012-11-12T14:13:48.887

Your Answer