views:

79

answers:

2

I'm trying to figure out how to load a new image with ajax when a link is clicked on.

The link looks like this (there are several links like this on the page they have little image thumbnails inside them so user can know approximately what image will load when he/she clicks on the link):

<div class="box-content2">
    <a href="/path/to/image/2.jpg" id="2">
        <img src="/path/to/image/thumbnail/2.jpg" title="Image Title 2" />
    </a>
    <a href="/path/to/image/3.jpg" id="3">
        <img src="/path/to/image/thumbnail/3.jpg" title="Image Title 3" />
    </a>
</div>

Href attribute contains relative path to the image and the id is the image name (also it's id in the database if it's relevant).

And on the same page I have this markup:

<div id="media-photo">
    <img src="/path/to/image/1.jpg" alt="Image Title 1" />       
</div>

I would like the image in #media-photo div to change without the page being reloaded.

This is what I have so far:

$(document).ready(function() {
    $('.box-content2 a').click(function() {
        var path = $(this).attr('href');
        $('#media-photo img').html('<img src="' + path + '" />');
    });
});

Not sure if what I'm doing is possible, maybe I need ajax?

+1  A: 

You're definitely on the right track:

$(document).ready(function() {
    $('.box-content2 a').click(function() {
        var path = $(this).attr('href');
        $('#media-photo img').attr('src', path)
                             .attr('alt', $('img', this).attr('title')); // in anticipation of your need
        return false; // stop the default behavior (following the link)
    });
});
Ken Browning
I tried that when I click on the link I'm taken to the image URL in the browser.
Richard Knop
You also need to remove the anchor tag <a href...> around your image or change the href to something like href="#" or href="javascript:void(0)" so that it doesn't actually link to the resource itself. Then your javascript here should work.
jkelley
@Richard Knop: I think you were too fast for my edit.
Ken Browning
jkelley's advice is correct if you choose not to return false from the click handler. If you're returning false then the href attribute can be any value you wish (in other words it doesn't have to be set to `#` or `javascript:void(0)`)
Ken Browning
@Ken: The image changes now. One last thing, the title is in the img tag inside the anchor, so I need to access that inside the click() event.
Richard Knop
@Richard Knop: I think I addressed that on line 5. Do I misunderstand?
Ken Browning
@Ken: Not entirely, because $(this) refers to the anchor. And the title tag is on the image inside the anchor but I can just add title attribute also to anchor and that solves that.
Richard Knop
As a side note - I'd recommend leaving the url in the anchor and canceling the click in the event handler. This way your users that have JavaScript disabled will be able to see the image.
Andy Gaskell
@Richard Knop: You're right, I've edited my answer.
Ken Browning
+1  A: 

The html method sets the html content of the element it's called on.

Your code is putting a new IMG element inside the old one, which is meaningless.

You need to to use the attr method to set the src attribute of the existing image, like this:

$('#media-photo img').attr('src', path);

Also, you need to add return false; to the end (or call event.preventDefault() and take an event parameter) to prevent the browser from following the link.

SLaks