Jump to content

Change Text And Background Color Of Field Based On Field Value


Recommended Posts

I have a text field with id=TextField. When the user types 10 into this field I would like the font color of 10 to change immediately to red (and if possible change the background color of the text field).

 

It seems the best way to do this is change the CSS style of TextField when 10 is entered into this field. 

 

I searched online but couldn't get any of the available code to work.

 

Thanks.

Link to comment
Share on other sites

To also change the background color you can have:

<script>
function f_color(){
if (document.getElementById('ID').value = '10') {
document.getElementById('ID').style.color = "red";
document.getElementById('ID').style.background="yellow";
}
}
document.getElementById('ID').onchange= f_color;
</script>

Make sure to replace ID in both codes

Link to comment
Share on other sites

Unfortunately I can't get this to work.

I tried both scripts (one at a time) by inserting them into an HTML block. here is what i inserted for the first script

 

<script>
function f_color(){
if (document.getElementById('EditRecordnote').value = '10') {
document.getElementById('EditRecordnote').style.color = "red";
}
}
document.getElementById('EditRecordnote').onchange= f_color;
</script>

 

 

Here is the input code: <input type="text" class="cbFormTextField" size="2" maxlength="255" name="EditRecordnote" id="EditRecordnote" value="10">

Link to comment
Share on other sites

First you need to put the script in the footer or after input tag, also this is calling the function onchange so if you have the value by default it will not work and only work when you type in 10 in the box and leave the box. If you would like to show it when the page loads then change it to:

<input type="text" class="cbFormTextField" size="2" maxlength="255" name="EditRecordnote" id="EditRecordnote" value="10">

<script>
function f_color(){
if (document.getElementById('EditRecordnote').value = '10') {
document.getElementById('EditRecordnote').style.color = "red";
document.getElementById('EditRecordnote').style.background="yellow";
}
}
f_color();
</script>
Link to comment
Share on other sites

  • 8 months later...
  • 2 weeks later...
  • 8 years later...

Hello - Just wanted to share another way to dynamically change the color of the calculated value/field when a condition is met using CSS.

You can insert this in the Header:

<style>
span.cbFormCalculatedField:has(+ input[name="cbParamVirtual1"][value="No"])  {
  color: #2543be;
}

span.cbFormCalculatedField:has(+ input[name="cbParamVirtual1"][value="Yes"])  {
  color: #29be25;
}

</style>

If you have more conditions or other fields, you can use this:

<style>
span.cbFormCalculatedField:has(+ input[name="cbParamVirtual1"][value="No"])  {
  color: #2543be;
}

span.cbFormCalculatedField:has(+ input[name="cbParamVirtual1"][value="Yes"])  {
  color: #29be25;
}

span.cbFormCalculatedField:has(+ input[name="InsertRecordFIELDNAME"][value="Inactive"])  {
  color: #2543be;
}

span.cbFormCalculatedField:has(+ input[name="InsertRecordFIELDNAME"][value="Active"])  {
  color: #29be25;
}
</style>

Hope it helps!

Link to comment
Share on other sites

  • 4 months later...

 Hi! You can also use this CSS code in other Form Elements:

Text Field and Email

<style>
input[name="InsertRecordFIELDNAME"][value="Active"] {
  color: green;
}
</style>


Display-Only field

<style>
span.cbFormData:has(+ input[name="InsertRecordFIELDNAME"][value="Active"]) {
  color: green;
}
</style>

 

Link to comment
Share on other sites

  • 6 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...