Forum Moderators: open
<a href="#" onlick="SOME CODE HERE"><img src="image.jpg"></a> <a href="#" onlick="SOME CODE HERE"><img src="image.jpg"></a>
This text appears when the above image is clicked.
<DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<div>
<a href="#" class="toggleLink">This link is not valid because it contains only # in the href</a>
</div>
<div>
<a href="#foo" class="toggleLink"><img src="image.jpg"></a>
<div id="foo">This 1st text appears when the above image is clicked.</div>
<div>
<div>
<a href="#bar" class="toggleLink"><img src="image.jpg"></a>
<div id="bar">This 2nd text appears when the above image is clicked.</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
$(document).ready(function () {
// Find all of the toggle links: they have class 'toggleLink', with an href
// that starts with '#' but is not equal to only '#' (protection against
// links like href="#" with no valid id). Get the elements with those ids
// and hide them
$("a.toggleLink[href^='#'][href!=#]").each(function (idx, el) {
var id = $(this).attr('href');
// Hide the element with this id
$(id).hide();
// Attach event handlers to the link
$(el).click(function (ev) {
ev.preventDefault();
var id = $(this).attr('href');
$(id).toggle();
});
});
});
</script>
</body>
</html>
href="#foo" class="toggleLink" in the <area> tag of an imagemap, but now the script won't work. The hidden text appears as already visible, and clicking the imagemap scrolls the page down to the div--basically it treats the div like an anchor, leading me to believe that the script can't "reach" the link from inside the image map. What have I done wrong?