Thursday, November 20, 2014

Stop IE8 Form Submit on HTML Button on click using JQuery

Internet Explorer 8, and also in newer versions of IE that are running in compatibility mode, buttons in a form automatically do a form post.

If you are in Chrome, newer versions of Internet Explorer or really any other modern browser this will not be a problem.  But often I have clients that use old versions of browsers for various valid business needs.

To get around this problem I just capture the click event in jquery and return false. The HTML and JQuery example below shows a snippet on how to do this.


<!DocType html>
<html>
<head></head>
<body>
<div id="create-div">
<form method="Post">
<button id="cancel-button">Cancel</button>
</form>
</div>
</body>
<script type="text/javascript">
$(function(){
$("#cancel-button").click(function(){
$("#create-div").hide(); // hide the div
return false; // return false to not submit the form
})
});
</script>
</html>
view raw Form.html hosted with ❤ by GitHub

No comments:

Post a Comment