Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set a Custom Icon for a jQueryUI Button

Is it possible to create a jQueryUI Button with a custom icon, ie: an icon that is not part of the sprite icons that are provided with jQueryUI???

I am using the ButtonSet functionality for a group of 3 checkboxes but need a more stylised icon than what is provided out of the box...

like image 908
Boycs Avatar asked May 14 '10 13:05

Boycs


2 Answers

Worked it out with a CSS hack.

Setup the button as per normal and give the primary icon the "Error" class defined below

.Error
{
    background-image: url('Images/Icons/16/Error.png') !important;
}

The !important overrides the ui-icon definition for background-image.

like image 130
Boycs Avatar answered Nov 09 '22 00:11

Boycs


I took this approach for one of my buttons and I discovered some interesting things:

  1. I didn't need to use the "!important" override
  2. I needed to set background-position for my button (otherwise I think the background was being displayed well outside the button)
  3. It looks like you can also put anything you like in the jQueryUI primary icon name - it just needs something as a placeholder.

For my uses I ended up with:

Javascript:

jQuery(function() {
    jQuery('#share-button').button({
        icons: { primary: "icons/share" }
    });

});

CSS:

#share-button > span.ui-icon {
    background-image: url(icons/share.png);
    background-position:0px 3px;}

HTML:

<button id='share-button'>Share</button>
like image 4
Ben Avatar answered Nov 09 '22 00:11

Ben