Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid a postback in JavaScript?

I have an ASP.NET page which has a button it it. The button click launches a modal dialog box using JavaScript. Based on the value returned by the modal dialog box, I want to proceed with, or cancel the post back that happens. How do I do this?

like image 907
ashwnacharya Avatar asked Sep 29 '08 18:09

ashwnacharya


2 Answers

Adding "return false;" to the onclick attribute of the button will prevent the automatic postback.

like image 173
Wayne Avatar answered Sep 30 '22 20:09

Wayne


Is this what you are trying to do?

<input type="button" id="myButton" value="Click!" />

<script type="text/javascript">
document.getElementById('myButton').onclick = function() {
    var agree = confirm('Are you sure?');
    if (!agree) return false;
};
</script>
like image 37
Joe Lencioni Avatar answered Sep 30 '22 19:09

Joe Lencioni