/    Sign up×
Community /Pin to ProfileBookmark

Javascript HEADACHE – break; erm… breaking it

Hi Guys

I am having a problem with my javascript at the moment but it makes absolutely no sense why it is going wrong.

I have a form and am using some javascript to validate it; seems simple so far.

Here is the validation code:

[CODE]
function checkForm(thisform)
{
//Check q1
elementname = “q1”;
check = false;
for (x=0; x<5; x++){
valuetocheck = document.getElementsByName(elementname)[x];
if (valuetocheck.checked == true){
check = true;
continue;
}
}

if (check == false){
alert(“Please answer question 1”);
break;
}
}
[/CODE]

Now the problem I am having is with that break; statement. When a user enters the page, they are shown a welcome message and a question of whether they would like to complete the survey. There are two links and if the user clicks no, they are navigated away from the page, if they click yes this javascript is activated through a hyperlink:

[CODE]<a href=”Javascript:void(0)” onclick=”toggleLayer()”>[/CODE]

which runs this code:

[CODE]function toggleLayer()
{
document.getElementById(“feedback”).style.display=”none”;
document.getElementById(“survey”).style.display=”block”;
}[/CODE]

which basically hides the welcome message and displays the hidden form.

If I include the break; statement in the code, this hyperlink doesn’t work; if I omit it, it does. Any ideas?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscApr 03.2009 — break is a reserved word used to 'break' out of a loop - stop the iterations when a certain condition is met. You aren't in a loop when you use the word break, what are you actually trying to accomplish at this point in the code:

<i>
</i> if (check == false){
alert("Please answer question 1");
break;
}


I think you are trying to prevent the form submission, in which case you have to return false (or use preventDefault() or returnValue = false depending on the browser).

HOWEVER, all that being said, I don't understand the relationship between the hyperlink that you posted and the checkForm function?

EDIT:

But perhaps your JS is throwing an error because there is a break statement in a funny place, in which case any other JS on the page won't work which is why your link isn't working - that's just a guess right now, though.
×

Success!

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