/    Sign up×
Community /Pin to ProfileBookmark

What’s wrong with this code?

Why does this code cause the page to scroll to the top? It doesn’t reload the page, but when I click a thumbnail to be viewed in the picture viewer on the right, the page automatically goes to the top even though the thumbnails and picture viewer are in the middle of the page? Thanks:

<script> var element_id = “”;
var state = 0;
function open_close ( id ) {
if ( state == 0 ) {
state = 1;
element_id = id;
document.getElementById( id ).style.display = ‘block’;
} else {
if ( id == element_id ) {
// state = 0;
// document.getElementById( id ).style.display = ‘none’;
} else {
state = 1;
document.getElementById( element_id ).style.display = ‘none’;
document.getElementById( id ).style.display = ‘block’;
element_id = id;
}
}
}
</script>

<a href=”#” onClick=”open_close(‘d28’);”><img src=”image” height=”100″ width=”100″ alt=”” border=”0″ hspace=”0″ /></a>
<div id=”d28″ style=”display:none”><img src=”image” /><br /></div>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisAug 07.2008 — &lt;a href="#" onClick="open_close('d28');[color=red]return false[/color]"&gt;
Copy linkTweet thisAlerts:
@WolfShadeAug 07.2008 — While gil davis might be right about adding the "return false;" to the onclick event, the culprit (to me) is the hash mark in the href. This reloads the page. What I usually do in cases like this is use "javascript:void(0);" for the href.
<i>
</i>&lt;a href="[color="red"]javascript:void(0);[/color]" onClick="open_close('d28');"&gt;

^_^
Copy linkTweet thisAlerts:
@firebladeAug 07.2008 — make it simple. No confusion

remove the <a> tag. make the following only

add onClick to the image.

<img src="image" height="100" width="100" alt="" border="0" hspace="0" [COLOR="DarkGreen"]onClick="open_close('d28');"[/COLOR] />
Copy linkTweet thisAlerts:
@kavicarterauthorAug 07.2008 — thank you very much. All answers were valid and worked perfectly.
Copy linkTweet thisAlerts:
@Declan1991Aug 07.2008 — While gil davis might be right about adding the "return false;" to the onclick event, the culprit (to me) is the hash mark in the href.[/QUOTE]
It does the same thing. The one good thing about using a link and "#" and the href, is that it is keyboard accessible. An onclick on the image is not.
×

Success!

Help @kavicarter spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...