views:

92

answers:

3

Hi everyone,

I am new to JavaScript (only a couple of days of reading a book) and I am stuck on this code snippet. I have looked at it over and over again but cannot seem to figure out why it is not working. I am sure it is something really simple that I have just looked over but I really just need a fresh pair of eyes to look at this.

The code is supposed to update a placeholder image on a page without the page having to reload. But when I am clicking on the link of an image, it is taking me to the link where the image is located instead of replacing the placeholder image. Here is my HTML code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Image Gallery</title>
<script type="text/javascript" src="scripts/showPic.js"></script>
</head>
<body>

    <h1>Snapshots</h1>
    <ul>
        <li>
        <a href="images/cat.jpg" onclick="showPic(this); return false;" title="A Cat">Cat</a>
        </li>
        <li>
        <a href="images/night.jpg" onclick="showPic(this); return false;" title="Night">Night</a>
        </li>
        <li>
        <a href="images/coke.jpg" onclick="showPic(this); return false;" title="Coke">Coke</a>
        </li>
        <li>
        <a href="images/sport.jpg" onclick="showPic(this); return false;" title="Sports">Sport</a>
        </li>
        <li>
        <a href="images/mnms.png" onclick="showPic(this); return false;" title="MnM's">MnM's</a>
        </li>
        <li>
        <a href="images/kid.jpg" onclick="showPic(this); return false;" title="A Kid">Kid</a>
        </li>
    </ul>

    <br />

    <img id="placeholder" src="images/placeholder.jpg" alt="Place Holder Image" />

</body>
</html>

And here is the JavaScript function I am using to get this done:

<script type="text/javascript">

function showPic(whichpic) { 
    var source = whichpic.getAttribute("href"); 
    var placeholder = document.getElementById("placeholder"); 
    placeholder.setAttribute("src",source);
}

</script>

Any help is greatly appreciated and thanks in advance for the help.

+1  A: 

You have two things trying to happen when you click on the hyperlinks:

  1. HREF navigation
  2. onclick event handler running

Your return false; effectively "stops" the event handler from doing anything else so you're good there. However, storing the image's address in the HREF is probably a bad idea. Try something like this instead:

<ul>
    <li>
    <a href="#" img="images/cat.jpg" onclick="showPic(this); return false;" title="A Cat">Cat</a>
    </li>
    <li>
    <a href="#" img="images/night.jpg" onclick="showPic(this); return false;" title="Night">Night</a>
    </li>
    <li>
    <a href="#" img="images/coke.jpg" onclick="showPic(this); return false;" title="Coke">Coke</a>
    </li>
    <li>
    <a href="#" img="images/sport.jpg" onclick="showPic(this); return false;" title="Sports">Sport</a>
    </li>
    <li>
    <a href="#" img="images/mnms.png" onclick="showPic(this); return false;" title="MnM's">MnM's</a>
    </li>
    <li>
    <a href="#" img="images/kid.jpg" onclick="showPic(this); return false;" title="A Kid">Kid</a>
    </li>
</ul>

with your javascript functiong being something like this:

function showPic(whichpic) { 
    var source = whichpic.getAttribute("img"); 
    var placeholder = document.getElementById("placeholder"); 
    placeholder.setAttribute("src",source);
}
Jaxidian
Hey Jaxidian, I have tried this new code but now all the script is doing is just updating the link in the url bar to http://example.com/image_gallery.html#. The picture I am clicking on is not being displayed on its own page or in place of the placeholder image. I really appreciate the reply. Do you have another solution? Thanks again.
Then you have some other problem (perhaps your file paths are wrong). This solution works. See here: http://jaxidian.org/so.htm
Jaxidian
WTF is the -1 for? This is a working solution... :-/
Jaxidian
Hey Jaxidian, Thanks the script is now working. :) The problem was the link to my javascript function: <script type="text/javascript" src="scripts/showPic.js"></script> For some reason, the function was not being called correctly and nothing was happening. When I put the function in the html file right under the body tag, it works great now. Thank you very much! Lol I was stuck for days. I am not sure why the -1 is there. I did not do that. I tried voting it up but it said I have to have at least 15 reputation points. Thanks again man.
Wasn't me who minused, but custom attributes on elements are generally not a good idea.
npup
@npup: Why? They seem like a pretty common practice from what I've seen.
Jaxidian
@three3 - If your question is answered, can you mark it as such please?
Jaxidian
If the solution works and the questioner is happy with it, then it should be voted up, or at least shouldn't be voted down. +1 from me.
Abbas
@Jaxidian For me it's not primarily about validation, even if that can be annoying too. @SpliFF mentions a few tings in his answer in [this thread](http://stackoverflow.com/questions/994856/so-what-if-custom-html-attributes-arent-valid-xhtml). I pretty much subscribe to his view. Moreover, there is already an attribute (`class`) that can serve the purpose for many of the cases where people resort to custom attributes. It's not just for style but also for "general purposes". [Here](http://www.w3.org/TR/html401/struct/global.html#adef-class)'s what the spec says about that. Best /npup
npup
@npup: I can definitely see that as being an acceptable viewpoint, but I cannot see that as being something that you should be going around as saying is *the right way* of doing it. Yes, it's a right way. Yes, you can argue that it's your preferred way. But there is not a clear right/wrong here. Now something I will buy is that maybe it's *more right* to use HTML5's data attribute previx (see answer here: http://stackoverflow.com/questions/994856/so-what-if-custom-html-attributes-arent-valid-xhtml )
Jaxidian
@Jaxidian Sorry if i stepped on a sore toe or something :) Didn't mean to "go around" *saying things* (God forbid). Yet.. when there are alternatives (that are even mentioned in the spec - currently `class` and in HTML5 `data`), it's hard for me to see the point in using custom attributes that (depending on your ability to see into the future) run the risk of clashing with future standardized attribute names.
npup
@npup: Oh, no worries with stepping on a sore toe. I'm enjoying this conversation! :-) I was just interpreting your "generally not a good idea" statement as "that's probably the wrong way". As for using `class`, yes, it's a *legal* way to do it but it just feels wrong to me. In all actuality, either way is probably a shortcut - our various HTML elements are not meant to be data structures to hold whatever data we want. For this we should use hidden input elements or dynamic javascript, right? :-P
Jaxidian
@Jaxidian Yeah I feel abit uneasy about storing "real" data in (non input) HTML elements. But when I "need to" I find some comfort in that line in the spec (*For general purpose processing by user agents*) and go for that when possible. This is opposed to inventing an attribute of my own to store stuff. What's happening in HTML5 is that `data` is suddenly used for something, by spec! Since it seems to be just a container, it might not really wreck anything, but it's an example of a custom attribute name that is probably pretty common (good name :)) , and suddenly becoming non-custom.
npup
A: 

Thanks Jaxidian. Worked Great. Solved.

As well as thanking the person who helped you out, please could you consider accepting that answer (click the empty tick mark besides the answer)? Thanks =)
David Thomas
I at least got a chuckle out of this! :-)
Jaxidian
A: 

The example looked like it should be working.

Though, when one gets unexpected form submit or unexpected following links in this way (unexpected because your form's onsubmit or your a's onclick has a return false; in it), it is often due to your javascript code raising an exception, and the return false; is skipped.

See:

<a href="www.example.com" onclick="alertOrFollow(this); return false;">alert or follow?</a>
<script type="text/javascript">
    function alertOrFollow(elem) {
        throw new Error('npup'); // try commenting this line out and things work as expected
        alert(elem.href)
    }
</script>

Your posted example looked fine i think, but maybe if your live version has some small differences? I'd suspect the placeholder element is not found, and setting the src attribute on it raises the exception that wrecks it.

npup