Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I dynamically change input textbox text color? 1

Status
Not open for further replies.

BillLumbergh

Programmer
Aug 15, 2001
51
US
I have a series of textboxes that are all related and are involved in the summarization of numbers. Putting a number in textbox A affects what is displayed in textbox B. If a change in textbox A makes the number in textbox B negative, I'd like to use Javascript to make the text in textbox B appear red. Then, if that number later becomes greater than or equal to zero, I'd like to turn it black again. I know how to use the style attribute of the input tag to set it to red initially; however, what I am wanting to do is, in the javascript function I have that recalculates the numbers, access that style attribute and set it to red or black as necessary. I'd guess this can be done, I just don't know the exact statement or syntax as to how to do it.

Also, I'd like it to work for both Netscape and IE, but I'm not expecting it to work in Netscape, so anything would be appreciated.

Thanks for any help.
 
You would need a function called from the Change event of textbox A:

Code:
<INPUT type=&quot;text&quot; name=&quot;textboxA&quot; onchange=&quot;changeB()&quot;>
<INPUT type=&quot;text&quot; name=&quot;textboxB&quot;>


Then the function would be:
Code:
changeB() {
  // first do your calculation to modify the value of textboxB
  if ( document.myform.textboxB.value < 0 ) {
    document.myform.textboxB.style.color = &quot;red&quot;
  }
  else {
    document.myform.textboxB.style.color = &quot;black&quot;
  }
}
I think that works, but I know that xutopia will probably correct me. :)I Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
That is exactly what I was looking to do. I was pretty close but didn't have the syntax exact. Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top