473,395 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to change background colour of form input field as soon as thevalue is changed?

Hi

How do I change the background colour of an input box in a form as soon
as the value is changed? Also the background should revert back to it's
original colour if the user decides that they do not want to make any
changes and hence retype back the orginial value.

I do not want the background to change after the user has moved to the
next form field but as soon as the value has been changed.

I am not sure how to start.

All your help will be appreciated.

Bundy

Oct 1 '06 #1
3 17302


Bundy wrote:

How do I change the background colour of an input box in a form as soon
as the value is changed? Also the background should revert back to it's
original colour if the user decides that they do not want to make any
changes and hence retype back the orginial value.

I do not want the background to change after the user has moved to the
next form field but as soon as the value has been changed.

You could use an onkeypress and/or onkeyup event handler and compare
this.value to this.defaultValue. Then you can set
this.style.backgroundColor as needed:

function setColor (control, color) {
if (control.style) {
if (control.value != control.defaultValue) {
control.style.backgroundColor = color;
}
else {
control.style.backgroundColor = '';
}
}
}

<input type="text"
value="initial value"
onkeypress="setColor(this, 'lightblue');"
name="inputName">
--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 1 '06 #2
Here's an idea...

You'd could capture the onBlur event (when the user LEAVES the input
field), and then check to see what the current value of that field is.
Compare it to the "default" value (the value you want it to be), and if
it's not a match, change the background color of the input field.

Bundy wrote:
Hi

How do I change the background colour of an input box in a form as soon
as the value is changed? Also the background should revert back to it's
original colour if the user decides that they do not want to make any
changes and hence retype back the orginial value.

I do not want the background to change after the user has moved to the
next form field but as soon as the value has been changed.

I am not sure how to start.

All your help will be appreciated.

Bundy
Oct 1 '06 #3
Thank you.

I have slightly modified the function provided by Martin Honnen and now
it works perfectly as I want it.

It was the onkeypress and onkeyup event handlers that I needed.

Regards

Bundy
Oct 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wim Roffal | last post by:
Is it possible to use javascript to change the name of a field in a form? Thanks, Wim
1
by: sman | last post by:
Hi, I recently read this article on About.com on how to create required fields for a form: http://javascript.about.com/library/scripts/blformvalidate.htm Everything works great except that there...
4
by: Semi Head | last post by:
Hello folks, I'm looking for a script to validate a specific number value in a standard form input field. An example would be, if someone enters a number into a form input, I want the script to...
10
by: yawnmoth | last post by:
i've written some javascript code that i believe should set a form variable to a certain value, depending on what the user clicks on. unfortunately, it isn't working. here's the url: ...
3
by: Peter Williams | last post by:
Hi All, I want to write some javascript for a html page which does the following. Imagine that the page contains a table with 2 columns and 3 rows, e.g.: +---+---+ | A | B | +---+---+
5
by: Garry Jones | last post by:
I have a form that I have designed with colors. I am using php code to color it depending upon other values. On my pc the colors are ok. From a friends pc every field with the word email in had...
4
by: Bosconian | last post by:
I've been fighting with this for an hour. My form contains a hidden input with the value initially set to "". When a user clicks on the link, a function is called that updates the hidden form...
3
by: helraizer1 | last post by:
Hi all, On one page of my site I have a form (code below) with an input textbox with the id "message". You type in your message in this field (as the id suggests) and depending on an option in a...
4
by: wizardry | last post by:
hello - i've created a form that has multiple inserts. it inserts the data fine if i manually parse the data to it but when i use the form to test the inserts it errors out. it errors out at...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.