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 post
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 post
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 post
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 post
Share on other sites
  • 8 months later...
  • 2 weeks 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...