Add colours to your Shuttle item
I wanted to allow users to select one or more colours from a list, and to control the order of the colours, so I’ve used a Shuttle item.
I wanted to have different background colours for each value in the list, so I started here for help. I’m not very strong with javascript (yet) but with a bit of looking around and playing I ended up with what I wanted.
I created an item on the page with the following attributes:
Display As: Shuttle
List of values definition: STATIC2:Yellow;#FFFF00, Green;#00FF00, Turquoise;#00FFFF, Pink;#FF90FF, Blue;#9090FF, Purple;#FF00FF, Red;#FF9090
Post Element Text:
<script type="text/javascript">
(function() {
for (i=0;i<$x("#CURRENT_ITEM_NAME#_2").length;i++) {
$x("#CURRENT_ITEM_NAME#_2")[i].style.backgroundColor
= $x("#CURRENT_ITEM_NAME#_2")[i].value;
}
for (i=0;i<$x("#CURRENT_ITEM_NAME#").length;i++) {
$x("#CURRENT_ITEM_NAME#")[i].style.backgroundColor
= $x("#CURRENT_ITEM_NAME#")[i].value;
}
})();
</script>
Notes:
- the value of each item in the list is a HTML colour code. This colour code is used to set the background colour of the item in the list.
- the shuttle item actually involves two select lists in the generated page. If the item name is P1_SHUTTLE, the generated items will be P1_SHUTTLE_2 (the left-hand list) and P1_SHUTTLE (the right-hand list). These are referenced in the javascript via #CURRENT_ITEM_NAME# and #CURRENT_ITEM_NAME#_2.
- the $x returns the select list dom object, which supports the “length” attribute – this returns the count of items in the list
- the select list index starts at 0 and goes up to length-1
A small problem is when the “reset” button is clicked the colours disappear. They reappear if the page is refreshed, however.