Chris Allington
Chris Allington

Reputation: 131

Trying to add a warning to prevent users from leaving a form before submitting. What am I doing wrong?

I have looked on here and around the internet for over 5 hours and I cannot figure out what I am doing wrong. It is probably something really simple. Here is my code so far:

<script type='text/javascript'>//<![CDATA[ 
window.addEvent('load', function() {
document.getElementsByClassName('submitformlisting')[0].onclick = function(){
window.btn_clicked = true;
};
window.onbeforeunload = function(){
if(!window.btn_clicked){
    return 'If you leave now, your information will be lost.';
}
};
});//]]>  

</script>

And my submit button looks like this:

<input type="submit" name="submit" id="submitformlisting" class="id="submitformlisting"" value="Add Listing" tabindex="4" onclick="return doSubmit();" />

Upvotes: 0

Views: 49

Answers (1)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382160

You messed the class in your html. Change class="id="submitformlisting"" to class="submitformlisting".

But you'd better use the id instead of the class.

Change

document.getElementsByClassName('submitformlisting')[0].onclick = function(){

to

document.getElementById('submitformlisting').onclick = function(){

Upvotes: 4

Related Questions