Replacing the Close icon for a JQueryUI Dialog box
Asked Answered
F

2

14

After extensive searching on this topic, I haven't been able to find an answer, so hopefully someone can help me with this issue. I have a relatively basic dialog box:

$("#dialog-search").dialog({
    resizable: false,
    height:dimensionData.height,
    width: dimensionData.width,
    modal: true,
    title: dimensionData.title,
    position: [x,y],
    open: function() {
        $("#dialog-search .dateField").blur();
    },
    close: function(event, ui){
       callBack(event,ui);
    }
});

What I want to do is replace the X icon (ui-icon-close) with a different icon provided by the ui (ui-icon-minus), so that clicking the minus icon closes the dialog instead. I've seen posts on how to hide the icon or replace it with a custom image in css, but I haven't yet found a way to replace the icon with another icon to perform the same functionality.

Edit: I also want to be able to use ui-icon-close for a different functionality in my dialog box by adding a custom behavior/location, but that may be outside the scope for this question. Feel free to address this if it's a related solution, though.

Fluorinate answered 26/10, 2011 at 21:28 Comment(0)
Q
15

Try to see the structure of the dialog and it should not be hard to do it.

http://jqueryui.com/demos/dialog/#theming

Use the create event to change the class of the close button icon to class of another icon will do.

http://jsfiddle.net/Quincy/kHU2M/1/

$("#dialog-search").dialog({
    create: function(event, ui) { 
      var widget = $(this).dialog("widget");
      $(".ui-dialog-titlebar-close span", widget)
          .removeClass("ui-icon-closethick")
          .addClass("ui-icon-minusthick");
   }
});
Quits answered 27/10, 2011 at 1:34 Comment(3)
Thanks, I'll check this out. It's really frustrating that javascript is so obtuse that you have to remove and add a Class just to accomplish a simple image replacement.Fluorinate
But how do I add my own image? To just create a CSS-class that uses background-image:... doesnt really work, it just messes the whole thing up. Is there a tutorial for exchanging the X with something else completely, not one of the "built in" images? And preferrably larger.Sliver
@Sliver I have added one more answer with custom image.Fuge
F
12

Old question, but maybe I'll help someone. Following CSS made the trick for me, totally custom Close button UI. Not very elegant :), but works fine for me.

.ui-icon-closethick {
    background-image: url(images/my-10px-image.png) !important;
    background-position: left top !important;
    margin: 0 !important;
}

.ui-dialog .ui-dialog-titlebar-close, .ui-icon-closethick {
    width: 10px !important;
    height: 10px !important;
}

.ui-dialog .ui-dialog-titlebar-close {
    background: none !important;
    border: none !important;
}

.ui-dialog .ui-dialog-titlebar-close, .ui-dialog .ui-dialog-titlebar-close:hover {
    padding: 0 !important;
}

My custom close button shown below:

Fuge answered 4/3, 2013 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.