/    Sign up×
Community /Pin to ProfileBookmark

Need serious help with form field variables

I have a web page that includes a quiz. I’m using javascript for validation. I did it a long time ago but now I want to update it and I’m pulling my hair out.

I have 868 lines of javascript and I KNOW it can be done with less than 12 lines.

Here’s a very short snippet. There’s actually 72 questions total.

[CODE]
<script type=”text/javascript”>
var answer1=”dead poets society”
var answer2=”unforgiven”

function validate1(quiz){
if (quiz.movie1.value == answer1)
{
quiz.movie1.style.backgroundColor=(‘#00FF00’); //Green
quiz.focus();
}
else if (quiz.movie1.value != answer1)
{
quiz.movie1.style.backgroundColor=(‘#FF3300’); //Red
quiz.focus();
}}
function validate2(quiz){
if (quiz.movie2.value == answer2)
{
quiz.movie2.style.backgroundColor=(‘#00FF00’); //Green
quiz.focus();
}
else if (quiz.movie2.value != answer2)
{
quiz.movie2.style.backgroundColor=(‘#FF3300’); //Red
quiz.focus();
}}
[/CODE]

Here’s the html.

[CODE]
<td><div align=”center”><img src=”1.jpg” width=”275″ height=”155″><br>
1. <input type=”text” name=”movie1″ onBlur=”this.value=this.value.toLowerCase(); validate1(quiz)”>
</div><p>&nbsp;</p></td>
<td><div align=”center”><img src=”2.jpg” width=”275″ height=”155″><br>
2. <input type=”text” name=”movie2″ onBlur=”this.value=this.value.toLowerCase(); validate2(quiz)”>
</div><p>&nbsp;</p></td>
[/CODE]

I tried calling the function by using validate(1); the using
function validate(nmbr) and it seem to work but I can’t figure out how to assign and call a variable for the form field name to change the background.

Here’s as far as I could get.

[CODE]
answer = new Array(“placeholder”,”dead poets society”)

function validate(nmbr){
if (quiz.movie1.value == answer[nmbr])
{
quiz.movie1.style.backgroundColor=(‘#00FF00’); //Green
quiz.focus();
}
else if (quiz.movie1.value != answer[nmbr])
{
quiz.movie.style.backgroundColor=(‘#FF3300’); //Red
quiz.focus();
}}
[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Mr_MooSep 11.2007 — var field = quiz["movie" + nmbr];
Copy linkTweet thisAlerts:
@motionsauthorSep 11.2007 — Well, I tried variations of that before but it's still not working. I'm sure it's because I'm not referencing something correctly. I actually tried to just do an alert() for the if statement and it won't do anything.

Here's what I have so far.

[CODE]<script type="text/javascript"><!--

answer = new Array("place holder","test")

function validate(nbr){

var field = quiz["movie"+nbr];

if (field.value == answer[nbr])
{
field.style.backgroundColor=('#00FF00'); //Green
quiz.focus();
}
else if (field.value != answer[nbr])
{
field.style.backgroundColor=('#FF3300'); //Red
quiz.focus();
}}

--></script>
[/CODE]
Copy linkTweet thisAlerts:
@motionsauthorSep 11.2007 — I found the solution. I had to add document in front. It works great. 868 lines of code down to 9 lines of code. Thanks Moo!

[CODE]<script type="text/javascript"><!--

answer = new Array("place holder","test")

function validate(nbr){

var field = document.quiz["movie"+nbr];

if (field.value == answer[nbr]){
field.style.backgroundColor=('#00FF00'); //Green
quiz.focus();
}
else if (field.value != answer[nbr]){
field.style.backgroundColor=('#FF3300'); //Red
quiz.focus();
}
}

--></script>[/CODE]

If your interested in the final page, here you go.

http://www.dvd-list.net/quiz

Scott
Copy linkTweet thisAlerts:
@Mr_MooSep 11.2007 — My bad. It must have been an early morning reply: the coffee hadn't kicked in yet.

Holy ****! Did you do the head removal on those pics? It's excellently done.
×

Success!

Help @motions 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,
)...