Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preventing postback 1

Status
Not open for further replies.

TeaAddictedGeek

Programmer
Apr 23, 1999
271
US
Is there any way to prevent an input button from causing a postback or the page to reload? I have input image button (regular HTML, not ASP.NET button) toggling a Javascript function to both change its image and toggle a div to visible versus not visible. It works just fine and you see it working--then the page reloads and it goes back to the defaults for both the input image button and the div.

Any ideas on how I could fix this?

Thanks in advance!

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
Is there a way to create an image that will react to an onclick event? I found one such example of an ASP.NET application which did this using a regular img tag, but I have NO idea how in the world they got it to have an onclick event. It certainly didn't work when I attempted it.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
I meant to add that you could just use the onclick inside of an image tag.

Code:
<img src="image.png" onclick="yourFunction();" />

or you could wrap it around an A tag

Code:
<a href="javascript:yourFunction();"><img src="image.png" /></a>

Ron Wheeler
Tekdev Open Source Development
 
The first example works. Thank you!! I thought that it was just the onclick event that wasn't working; it was actually the original Javascript I was attempting to use.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
You can use an image button. All you need to do is return false from the onclick event. This prevents the submit which can be handy for doing client side validation.

<input type="image" src=" onclick="doSomething()">

<script>
function doSomething()
{
// what ever you want to do.
return false;
}
</script>
 
Image buttons do postbacks and the Javascript changes it's trying to set gets erased. The regular image button was what did the trick. Thanks again!

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top