4

How can I do something like:

$("#some_div").dialog("doSomething");

And what that method should do is to add an extra icon in the titlebar

EDIT 1: I've tried this solution: the method gets called but I can't access the dialog object (maybe I'm doing something wrong)

1
  • @Ohgodwhy: I've updated my question to show what I've tried so far Commented Oct 1, 2012 at 20:22

2 Answers 2

2

First, if you're adding an icon to the title bar I would suggest applying a class to that dialog box and styling it with CSS. Example:

$( "#some_div" ).dialog({ dialogClass: "someClass" });

If you'd still like to add a custom method, here's what the documentation says:

Supply a callback function to handle the create event as an init option.

$( ".selector" ).dialog({
   create: function(event, ui) { ... }
});

Bind to the create event by type: dialogcreate.

$( ".selector" ).bind( "dialogcreate", function(event, ui) { 
    ... 
});
Sign up to request clarification or add additional context in comments.

2 Comments

the "create" approach is for use that event when the dialog is created. I need to add an icon at run time.
Should have clarified that in your question - glad you solved your problem.
2

Ok, this is what I did:

//in a separate js file
$.ui.dialog.prototype.showExtraButton = function()
{
    this.uiDialogTitlebar.append("<a role='button' class='ui-dialog-titlebar-icon ui-dialog-titlebar-icon-extra'><span class='ui-icon'></span></a>");
}

//call it this way
$("#some_div").dialog("showExtraButton");

//css
.ui-dialog-titlebar-icon {
  position: absolute;
  right: 25px;
}

.ui-dialog-titlebar-icon-extra span
{
    display: block;
    background-image: url(../path_to_img/button_extra.png)!important;
}

This solution by Langdon, along with this one by Kevin B gave me the answer on how to resolve my problem

UPDATE 2014-01-03

TIL about $.widget(), so here is another implementation of the same thing

$.widget("ui.dialog", $.ui.dialog,
{
    showExtraButton: function()
    {
        this.uiDialogTitlebar.append("<a role='button' class='ui-dialog-titlebar-icon ui-dialog-titlebar-icon-extra'><span class='ui-icon'></span></a>");
    }
});

1 Comment

Side Note: Try separating form from function. Unless that anchor (that's wrapping the <span class='ui-icon'></span> serves a specific purpose, you're probably better off without it (i.e.: you can use a different tag instead).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.