...

View Full Version : How do you change bg color when Text is auto filled into text Field?



chris77780124
06-18-2012, 09:28 PM
So I have figured out from a guy on here that this code lets you change the BG Color when certain text is enter into a text field. It works just fine.

But now what I need help with is what Java Script statement do I use if the field has text automatically enter into the field. I need the same thing to happen the BG Color to change a different color depending on what text has been auto filled.

If someone knows what extra code I would need to enter to make this work
Thanks to all who can help.

<body>
<input type="text" name="fld1" id="fld1" onkeyup="changeBG(this.value)"/><br />
<input type="text" name="fld2" id="fld2" onkeyup="changeBG1(this.value)"/><br />
<input tupe="text" name="fld3" id="fld3" onkeyup="changeBG(this.value)"/><br />
<script>
function changeBG(val){
var colors={"AMDML":"red","MA":"green","SPN":"black"};
for(i in colors){
if(val.match(i)){
document.body.style.backgroundColor=colors[i];
}
}
}
</script>

<script>
function changeBG1(val){
var colors={"AMDML":"blue","CA FANNIEMAE MA":"green","BCRA ETRADE":"black"};
for(i in colors){
if(val.match(i)){
document.body.style.backgroundColor=colors[i];
}
}
}
</script>

</body>

WolfShade
06-18-2012, 09:41 PM
Use the windows.onload command to run the function supplying the value (document.forms["formname"].fld1.value).

chris77780124
06-18-2012, 10:26 PM
I'm confused you want me to replace the document.body.style.backgroundColor=colors[i]; with the code you wrote?
document.forms["formname"].fld1.value

Let me know if I'm correct. Thanks

\<script>
function changeBG1(val){
var colors={"AMDML":"blue","CA FANNIEMAE MA":"green","BCRA ETRADE":"black"};
for(i in colors){
if(val.match(i)){
document.forms["formname"].fld1.value

}
}
}
</script>

xelawho
06-19-2012, 12:09 AM
it will depend on how the text "automatically" gets filled. text inputs have an onchange event, but this doesn't fire until the input loses focus (so is kind of useless imo) - you will probably have to fire the function from the function that sends the text, but without seeing that, it's hard to say.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum