Well i have a asp.net page where i have a button that im using to execute a JavaScript... but after the JavaScript has run the page reloads(postback) how can i avoid that?
<button onclick="printElement('content');">Print</button>
you need to use return false
<button onclick="printElement('content');return false">Print</button>
onclick="return printElement('content');" then you CAN use the return value, true or false, from the function. You just need to return something from the onclick event itself (which is itself wrapped up in a function before exection), or it defaults to true.
Sep 19, 2013 at 2:22
Add type="button" to your <button> tag like so:
<button type="button" onclick="printElement('content');">Print</button>
This answer explains the behavior.
return false does nothing and is not needed. type=button is essential.
Alter you click event as below:
<button onclick="printElement('content');return false;">Print</button>
if you don't need a post back you should use an HTML button.
in that case you don't need override the behavior to avoid the post back and it's laighter to load (it doesn't need to be worked by the server to render back the HTML)
You should just use an input button instead:
<input type="button" onclick="printElement('content')" value="Print" />
This doesn't NEED an form (so you can use it without) and you don't have the problem with the postback.
return printElement('content');