1

Here's a TextBox:

<asp:TextBox ID="UsernameTextBox" runat="server" class="BlockInput"></asp:TextBox>

I wanna give an asp textbox some color but its not changing color

 <script type="text/javascript">
     function BlockInput() {
         var elements = document.getElementsByClassName("BlockInput");
         for (var i = 0; i < elements.length; i++) {
             elements[i].readOnly = true; // works
             elements[i].style.color = "#000000";// does not work!
         }
     }
     window.onload = BlockInput;
</script>

When I view source I could see the style being added:

<input name="ctl00$MainContent$UsernameTextBox" 
       type="text" id="MainContent_UsernameTextBox"
       class="BlockInput" readonly="" style="color: rgb(0, 0, 0);">

I also made sure nothing is overwriting the css.

meda
  • 45,103
  • 14
  • 92
  • 122

3 Answers3

3

change

elements[i].style.color = "#000000";

to

elements[i].style.backgroundColor= "#000000";

hope this will help you.

<script type="text/javascript">
     function BlockInput() {
var elements=document.getElementsByTagName('input');
         for (var i = 0; i < elements.length; i++) {
             elements[i].readOnly = true; // works
             elements[i].style.backgroundColor= "#000000";
         }
     }
     window.onload = BlockInput;
</script>
Kiranramchandran
  • 2,094
  • 16
  • 30
1

The variable elements is undefined. Do you want to make something like ? :

function BlockInput() {
         var elements = document.getElementsByTagName('input');
         for (var i = 0; i < elements.length; i++) {
             elements[i].readOnly = true; // works
             elements[i].style.color = "#000000";// does not work!
         }
     }

Fiddle

Paul Rad
  • 4,820
  • 1
  • 22
  • 23
  • Your answer was very close, TagName needed to be uppercase, but I will give you upvote – meda Sep 30 '13 at 14:49
0

With the color style you are setting the text color to black. If you want to change the background color of the textarea, try this:

elements[i].style.backgroundColor = "#000000";
Speedy
  • 198
  • 1
  • 8