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

Getting listboxitem background color in javascript

3 Answers 228 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Eddie
Top achievements
Rank 2
Eddie asked on 08 Oct 2009, 02:43 PM
Hello all,

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

Thanks!

Eddie

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 09 Oct 2009, 05:23 AM
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.
0
Abhay
Top achievements
Rank 1
answered on 24 Feb 2011, 10:59 AM
Is it possible to change clour of rad listbox, when it is empty or it contens no record.
0
Shinu
Top achievements
Rank 2
answered on 24 Feb 2011, 01:16 PM
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.
Tags
ListBox
Asked by
Eddie
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Abhay
Top achievements
Rank 1
Share this question
or