51

http://jqueryui.com/upgrade-guide/1.10/#changed-title-option-from-html-to-text

jQuery UI 1.10 made it so that the dialog title can only be text (no html) to prevent scripting vulnerabilities. I'm not allowing user input to generate this title, so I would still like to use HTML, mainly to display an icon to the left of the title.

I'm going to post my solution to this problem because I haven't seen anyone else ask or answer this yet. Hopefully it will help someone else, or someone else may have a better approach.

More info as to why they did it: http://bugs.jqueryui.com/ticket/6016

0

3 Answers 3

80

This will override the function used when setting jQuery UI dialog titles, allowing it to contain HTML.

$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
    _title: function(title) {
        if (!this.options.title ) {
            title.html(" ");
        } else {
            title.html(this.options.title);
        }
    }
}));
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, at one point i was setting the html manually via attributes, that fixes this problem, thanks
15

If you hesitate to override jQuery's _title method, you can use the html, append, or similar methods on the title element at the jQuery dialog's open event, like so:

$("#element").dialog({
  open: function() {
    $(this).find("span.ui-dialog-title").append("<span class='title'>" + subtitle + "</span>");
  }
});

The above parses the HTML correctly while bypassing jQuery's title method. And since it happens at the open event, the user experience remains seamless. Just did this on a project, and it worked beautifully.

7 Comments

If you use open it will keep appending. In this case the create is the right event. api.jqueryui.com/dialog/#event-create
Whether or not you'd use the create function, ADD a scope of somekind!! e.g. create: function () { $(this).siblings().find(".ui-dialog-title").html('<strong>my html title</strong>'); }
@Rudy Create would do it too, but open was what worked best in that particular case. Whether using open, create, or some future, yet-written option, the method of choice for this is more about the particular implementation...unless there's something I'm missing about open that makes this a serious no-no. If there is, do tell.
@321X Agreed. I cut off too much in trying to create a simple example. I'm editing my response now. TY for catching it.
I had to wrap it in a setTimeout, but it works -- thanks!
|
1

This will modify the title after init the dialog

$('#element').dialog(options);

var dialogTitle = $('#element').closest('.ui-dialog').find('.ui-dialog-title');
dialogTitle.html('<strong>hello world</strong>');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.