This is a migrated thread and some comments may be shown as answers.

Changing the background colour of the selected item

2 Answers 150 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Bex
Top achievements
Rank 1
Bex asked on 02 Jul 2012, 03:11 PM
Hi

I have been reading this thread:http://www.telerik.com/community/forums/aspnet-ajax/combobox/combo-box-color-picker.aspx 

As I want to change the background colour of the selected item when an item in my dropdown is selected. 
It gives this bit of javascript to do the job, but it's obviously very old as no longer works in the same way.

function onClientSelectedIndexChanged(item) 
    var combo = item.ComboBox; 
    var comboInput = document.getElementById(combo.InputID); 
    //here ItemColor is a custom attribute that you can assign in ItemDatabound event 
    comboInput.style.backgroundColor = item.Attributes.ItemColor; 
</script> 


I have changed it to:
function onClientSelectedIndexChanged(sender, eventArgs) { 
       var combo = eventArgs.get_item();
       var comboInput = document.getElementById(combo.InputID);
       //here ItemColor is a custom attribute that you can assign in ItemDatabound event
       comboInput.style.backgroundColor = item.Attributes.ItemColor;
   }

but the  "document.getElementById(combo.InputID); " does not work, as i expected it wouldn't but I don't know what the replacement if it is?

Can anyone help?

Bex

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 03 Jul 2012, 03:55 AM
Hi Bex,

Try the following code snippet to change the background color of the selected item.

JS:
<script type="text/javascript">
    function OnClientSelectedIndexChanged(sender, args) {
        var combo = args.get_item();
        var comboInput = sender.get_inputDomElement();
        comboInput.style.backgroundColor = item.Attributes.ItemColor;
    }
</script>

Hope this helps.

Thanks,
Princy.
0
Bex
Top achievements
Rank 1
answered on 03 Jul 2012, 08:50 AM
Hi Princy!

Thank's that;s great!

I had to update it a bit, but it now works a treat!

<script type="text/javascript">
    function OnClientSelectedIndexChanged(sender, args) {
        var item = args.get_item();
        var comboInput = sender.get_inputDomElement();
        comboInput.style.backgroundColor = item._element.style.backgroundColor;
    }
</script>
Tags
ComboBox
Asked by
Bex
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Bex
Top achievements
Rank 1
Share this question
or