/    Sign up×
Community /Pin to ProfileBookmark

Change back color of textbox when text is present??

Does anyone know if it is possible to change the color of a textbox when text is present? I have a .net page that returns info from a database and populates textboxes. I would like to change the color of the textbox if text is present. I cannot use the onfocus event because a script is entering the text not the user. Anyone have any ideas??

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@kelly23Feb 02.2008 — Hi,

My javascript knowledge is very basic, but something like this should work:

<i>
</i>&lt;html&gt;
&lt;title&gt;&lt;/title&gt;
&lt;head&gt;
&lt;script language="javascript" type="text/javascript"&gt;
function changeBgColor(){
for (i=0; i&lt;document.form1.elements.length; i++){
if (document.form1.elements[i].value != "" &amp;&amp; document.form1.elements[i].type != "submit"){
document.form1.elements[i].style.backgroundColor="yellow";
}
}
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body onLoad="changeBgColor()"&gt;
&lt;form action="whatever" name="form1"&gt;
First Name: &lt;input type="text" name="fName"&gt;&lt;br&gt;
Last Name: &lt;input type="text" name="lName"&gt;&lt;br&gt;&lt;br&gt;
&lt;input type="submit"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;


hth

Kelly
Copy linkTweet thisAlerts:
@JMRKERFeb 02.2008 — Could also try this:
[code=php]
<html>
<head>
<title>Text Alert</title>
<script type="text/javascript">
function CheckEmpty(IDS) {
var obj = document.getElementById(IDS);
if (obj.value == '') { obj.style.backgroundColor = '#ffffff'; }
else { obj.style.backgroundColor = '#ffff00'; }
}
</script>
</head>
<body>
Student: <input type="text" id="info1" size="10" onBlur="CheckEmpty(this.id)">
Patient: <input type="text" id="info2" size="10" onBlur="CheckEmpty(this.id)">
<p />
Sphere: <input type="text" id="sph" size="5" onBlur="CheckEmpty(this.id)">
Cylinder: <input type="text" id="cyl" size="5" onBlur="CheckEmpty(this.id)">
Axis: <input type="text" id="axis" size="5" onBlur="CheckEmpty(this.id)">
</body>
</html>
[/code]

Good Luck! ?
Copy linkTweet thisAlerts:
@codingisfunFeb 02.2008 — JMRKER, can't we use

function CheckEmpty(obj) {

if (obj.value == '') { obj.style.backgroundColor = '#ffffff'; }

else { obj.style.backgroundColor = '#ffff00'; }

}

and

Student: <input type="text" size="10" onBlur="CheckEmpty(this)">

ID is not required.
Copy linkTweet thisAlerts:
@JMRKERFeb 02.2008 — 'codingisfun': Don't see why not.

I just most often have use of the ID or NAME

and therefore almost always add them automatically.
×

Success!

Help @dkirk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...