Thursday, 28 July 2011

Cancel submit event using JavaScript

Usually when working with AJAX, you need to prevent the form submission from actually happening to prevent the page from refreshing and may be going to a URL which can only serve AJAX calls. The sample below shows how to use event.preventDefault() to stop the click action from propagating. Take care from having a JS coding error just before event.preventDefault() as the method execution in such case will halt and form submission will not stop.

<input name="gift_card_verify" id="gift_card_verify" type="submit" onclick="verifyGiftCard(event);"/>

<script type="text/javascript">
function verifyGiftCard(event) {
//Do your logic for example submit an AJAX request
event.preventDefault();
}
</script>

No comments:

Post a Comment