Second Life of a Hungarian SharePoint Geek

October 24, 2011

Creating a ToggleButton on SharePoint ribbon with alternating images

Filed under: jQuery, Ribbon, SP 2010 — Tags: , , — Peter Holpar @ 11:17

Although ToggleButton is a useful control on the ribbon of SharePoint 2010 user interface, unfortunately I found no built-in way to assign separate images to the on / off states of the button.

After playing awhile with the Internet Explorer Developer Tools (F12, see also the Tools menu in IE 8), I find out how to achieve that through the HTML object model built up by the ribbon infrastructure.

The following JavaScript code is from the command handler method of the custom ribbon components and assumes jQuery is already loaded.

Replace the commandId of the ToggleButton and the image properties (src and style attributes) to match your needs.

  1. if (commandId === 'Ribbon.Custom.List.MyFeature.SwitchOnOff')
  2.     var x = jQuery("a[id|='" + commandId + "']").find("img");
  4.     if (properties.On) {
  5.         x.attr("src", "/_layouts/images/MyImages/On.png");
  6.         x.attr("style", "top: 0px; left: 0px;");
  7.     }
  8.     else {
  9.         x.attr("src", "/_layouts/images/MyImages/Off.png");
  10.         x.attr("style", "top: 0px; left: 0px;");
  11.     }

You can use separate image files, or a single one with different vertical / horizontal offset value.

I’ve defined a global switchedOn variable to track the actual status of the ToggleButton easier, and query that value from other JavaScript methods:

var switchedOn = false;

I hope this trick helps you to make your SharePoint UI a bit more dynamic.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: