Reputation: 1311
I have an image that I want it to act like a button to preform an java-script onclick on it, everything is working fine except when I click over the image it returns to the top of the page, I want it the page to stay on the same place that I am scrolling (middle of the page for example) when I click over the link image. This is my code
<a id="dell" href="#"><img src="images/logo80.png" width="80" height="80" alt="Dell" /></a>
Upvotes: 0
Views: 90
Reputation: 2232
You don't need to surround it with an
<a>
tag, this is what taking you to the top of the document (href="#").
If you want to set the cursor to be a hand icon do it through css:
img { cursor: hand; cursor: pointer; }
Upvotes: 0
Reputation: 981
Here is an example using inline javascript.
<a id="dell" href="#" onclick="alert('hello world!'); return false;"><img src="http://static.adzerk.net/Advertisers/37332fa8c2654aef804e417e3788e977.png" width="80" height="80" alt="Dell" /></a>
Upvotes: 0
Reputation: 153
Well the problem is just about your tag. You've set an href='#' which means "reloading", that is why the browser brings you up to the page.
In order to set a javascript event on click try the following:
Otherwise there are other methods like the property onclick='nameOfYourFunction()' or jQuery events.
Upvotes: -3
Reputation: 3531
You have to prevent the default action for the a tag or it will use the href.
Try this in your event handler.
$(document).ready(function() {
$("#dell").click(function(e) {
e.preventDefault();
$("#boarder").toggle(500);
});
});
Another thought is that you actually don't need an a tag if you aren't using it to navigate somewhere.
Upvotes: 5