After alert click, go to anchor text in site

I have an alert that is checking if a form has been filled in, and basically I want to add some extra feedabck so that when the click the ‘OK’ of the alert button they will travel to that position in the page.

This is what I got so far

else if (document.form2.primarycause.value == "0")
alert("Most likely or actual primary and secondary outcome - Please complete all required fields");
return false;

And also add ‘1’ to value of a variable in php.

$red_line = "";

If the element in question has an id you can set window.location.hash to achieve this:


Or, is you want to do something a little more complex, find out the position of the element on the page and animate the jump to this point (maybe with the jQuery scrollTo plugin)

Hi Pullo,

No thats perfect, it works great.

What is the usual method of highlighting a required form element.

I thought about using a variable in php and then using an if statement to underline the label in red, but when I tried to make the variable a different value as below, it didnt quite do what I needed.


if (document.form2.hazardornearmiss.value == "0")
    alert("Hazard or Near Miss? - Please complete all required fields");
	<? $red_Line="1"; ?>
	return false;

<td width="193px" style="vertical-align:top;">
<a name="Hazard" id="Hazard"></a>
<? if ($red_Line=="") { ?>
<span style="color:#900">*</span> Hazard or near miss?
<? } else { ?>
<span style="color:#900">*</span><span style="border-bottom:#990000 solid 1px">Hazard or near miss?</span>
<? } ?>

Its very basic I know, but to me it doesnt seem right.

Hi there,

I would assign this element a class of “field_with_errors” or something similar.
You can do this in both your JS and PHP, then style it accordingly.

I would also put a small summary of why the form submission was not successful somewhere near the form.