Click to See Complete Forum and Search --> : Fun with Onclick


netfrugal
01-13-2008, 04:22 AM
I have a few forms on my php pages. They all have a nice little onclick event placed within each submit button. Nothing special. I basically stop a person from NOT finishing their form entries - thus reminding them to finish the submissions.
I have a series of forms for entering data in a sequential order. So, if someone leaves the page, without completing the all forms on all pages, then I need to alert them that all data will be lost from their previous forms.


<input type="submit" value="next page" onclick="needToConfirm = false;">


When the button is clicked, then the variable "needToConfirm" is obviously false. Which tells the js in the header that all is ok, don't create a popup alert.

Here's the code in the header:


<script language="JavaScript">

var needToConfirm = true;

window.onbeforeunload = confirmExit;
function confirmExit()
{
if (needToConfirm) return "If you leave now, all data you entered will be lost.";

}
</script>


Some pages have multiple forms, and I don't want to add the onclick event to every button everytime I create a new form. And for that matter, any hyperlinks on my pages basically do the same thing.

Is there a more Global way for js to detect all button/link elements on a page? Can js detect them is this fashion?

Weedpacket
01-13-2008, 06:33 AM
document.getElementsByTagName()