Changing the background colour of the selected item

3 posts, 1 answers
  1. Bex
    Bex avatar
    53 posts
    Member since:
    Jul 2011

    Posted 02 Jul 2012 Link to this post

    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. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 02 Jul 2012 Link to this post

    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.
  3. Bex
    Bex avatar
    53 posts
    Member since:
    Jul 2011

    Posted 03 Jul 2012 Link to this post

    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>
Back to Top