/    Sign up×
Community /Pin to ProfileBookmark

How to get the background color of a form field

I have built a spry widget form where the submit button is at the bottom of the page, and all my required fields are at the top of the page. The problem for a user is that when it is submitted and there is an error in a required field, it seems that nothing happens. Only by scrolling back up to the top of the page will they see the error field.
What I would like to do is test for the background color of the field and if it is not the good field color, then have an alert tell them which field is invalid.
The code I’m trying occurs on the submit operation.

My form is named addForm. Here is what I have.
<form method=”POST” action=”Admin/DataSave.html” name=”addForm” onSubmit=”return fieldErrors()”>

This is the function to run on the onSubmit.
The <span id for the field where you enter your name is sprytextfield1. If this field validates then it has the background color of #B8F5B1.

<script type=”text/javascript”>
<!–
function fieldErrors() {
var theBgd = document.getElementById(“sprytextfield1.style.backgroundColor.value”);
If(theBgd != “#B8F5B1”)
alert(“Oops, there is an error with your name.”);
return false;
}
//–>
</script>

Where am I going wrong?
I’m a newbee to scripting.
Thanks for any help.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jul 03.2010 — If it's a name, var theBgd = document.getElementsByName("sprytextfield1").style.backgroundColor;

If it's an id var theBgd = document.getElementById("sprytextfield1").style.backgroundColor;

That presumes it's an inline style.
Copy linkTweet thisAlerts:
@deweyhtuckerauthorJul 03.2010 — This is not an inline style but one that the spry widget pulls from a linked css file.
×

Success!

Help @deweyhtucker 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 5.27,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...