Getting listboxitem background color in javascript

4 posts, 0 answers
  1. Eddie
    Eddie avatar
    11 posts
    Member since:
    Jun 2007

    Posted 08 Oct 2009 Link to this post

    Hello all,

    Is it possible to get and/or set the background color of a radlistbox item in Javascript? 

    Thanks!

    Eddie
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 09 Oct 2009 Link to this post

    Hi Eddie,

    Here is the example which shows how to set/get the background color of RadListBoxItem from clientside.

    aspx:
     
    <telerik:RadListBox ID="RadListBox1" runat="server"
        <Items> 
            <telerik:RadListBoxItem runat="server" Text="RadListBoxItem1" /> 
            <telerik:RadListBoxItem runat="server" Text="RadListBoxItem2" /> 
          . . . 
        </Items> 
    </telerik:RadListBox> 
    <input id="Button1" type="button" value="set" onclick="setColor();" /> 
    <input id="Button2" type="button" value="get" onclick="getColor();" /> 

    javascript:
     
    <script type="text/javascript"
        function setColor() { 
            var listBox = $find('<%= RadListBox1.ClientID %>'); 
            var item = listBox.findItemByText("RadListBoxItem1"); 
            item.get_element().style.backgroundColor = "red"
        } 
        function getColor() { 
            var listBox = $find('<%= RadListBox1.ClientID %>'); 
            var item = listBox.findItemByText("RadListBoxItem1"); 
            alert(item.get_element().style.backgroundColor); 
        } 
    </script> 

    -Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Abhay
    Abhay avatar
    5 posts
    Member since:
    Nov 2010

    Posted 24 Feb 2011 Link to this post

    Is it possible to change clour of rad listbox, when it is empty or it contens no record.
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 24 Feb 2011 Link to this post

    Hello Abhay,

    You can try the following client side code by subscribing the RadListBox event event OnClientLoad.

    Here is the code which fired on the ClientLoad
    ClientSide:
    function OnClientLoad(sender, args)
       {
          var listbox = $find("<%= RadListBox1.ClientID %>");
          if (listbox.get_items().get_count() == 0)
         {
              listbox.get_element().style.backgroundColor = "Red";
          }
      }

    Thanks,
    Shinu.
Back to Top