0

So - jQuery UI library. There is a dialog component. There is also a "title" option for this component. How would I override jQuery's default functionality so that whenever I set a new title for a dialog - it adds "xxx" in front of it?

Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
Eriks
  • 306
  • 2
  • 14

3 Answers3

1

I would write a helper method to set the title and call that instead:

function setTitleWithPrefix(selector, title) {
    $(selector).dialog('option', 'title', 'xxx' + title);
}

You could even create it as a jQuery plugin:

$.fn.dialogTitleWithPrefix = function(title) {
     this.each(function() { 
         $(this).dialog('option', 'title', 'xxx' + title);
     });
};

Which can be called as:

$('.myDialog').dialogTitleWithPrefix('New Title');

Working example of both methods - http://jsfiddle.net/kReA5/1/

Alternatively, look at the answer to this question (How to extend an existing jQuery UI widget?) if you want to extend the dialog widget itself.

Community
  • 1
  • 1
Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
0

You can override the jQuery.dialog method

var oldDialog = jQuery.fn.dialog;
jQuery.fn.dialog = function(title, options, otherStuff) {
    title = 'xxx' + title;
    return this.oldDialog(title, options, otherStuff);
});

However, I don't know what other options you use with this method. If you provide some code to your question I'll update my answer.

matewka
  • 9,912
  • 2
  • 32
  • 43
0

Well this will render your dialog:

<div id="dialog" title="Your title">
  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>

It will render it with the selected title however if you now also wish to prepend xxx to the title you will need to change the title attribute value after that div.

To do this use the following script:

<script>
  var title = $("#dialog").getAttribute("title"); // This will return "YOUR TITLE"

  // Now we change the title to xxx YOUR TITLE
  $("#dialog").attr("title", "XXX" + title);
</script>
Sterling Duchess
  • 1,970
  • 16
  • 51
  • 91