views:

22

answers:

3

Should the following code not work?

$('.elastica').click(function(){
    document.getElementById('bigimage').attr('src')= $(this).attr('src');
});

It doesn't change a thing on the site. I've also tried this outputting the image location trough the the other image's id, but it comes up empty (literally ""). When I output the other image's src to a div box with innerHTML for texting, it comes up as undefined.

Either way it won't update bigimage's src.

A: 
$('.elastica').click(function(){
    $('#bigimage').attr('src', $(this).attr('src'));
});
Nirmal
you should go jQuery all the way: $('#bigimage').attr('src', $(this).attr('src'));
Shay Erlichmen
I corrected it immediately after I realized it.
Nirmal
Thanks, this was it, I will accept your answer when I'm allowed. I wish I could choose you twice, for the very swift answer! There was only one problem...$(this).attr('src') is an anchor, which has no src. To fix this, I used $(this).find('img').attr('src');
serv-bot 22
Nirmal, you should edit your code so as not to confuse someone who came here looking for the answer. The anchor you are using has not src attribute.
serv-bot 22
@serv-bot 22: Your question does not mention what element the click function is applied to. An onclick event can be even applied to an image. I shall leave the answer as long as you don't change the question.
Nirmal
To refer to the `src` of the `img` element inside the anchor, you can use `$("img", this).attr("src");`. The second parameter `this` will tell jQuery in which context the img element should be searched.
Nirmal
You're right. I did not specify whether the 'elastica' class applied to images or div boxes.Also, $("img", this).attr("src"); gets converted to $(this).find('img').attr('src'); by jquery to my understanding; making $(this).find('img').attr('src'); a bit faster. Both are valid and the amount of time spent converting is not even worth considering, so deciding which to use is trivial.
serv-bot 22
A: 

document.getElementById('bigimage') isn't a jQuery object so you cant use jquery function attr on it. You can do it like this:

$(document.getElementById('bigimage')).attr('src', $(this).attr('src'));

but you can also use jquery selector engine to get #bigimage:

$('#bigimage').attr('src', $(this).attr('src'));
giolekva
I just figured it out on my own. I was using the anchor, which has no src.To fix this, I used $(this).find('img').attr('src');
serv-bot 22
A: 

By calling

document.getElementById('bigimage')

you receive the native DOM object which doesn't know about .attr() which is a jQuery method. You can either call

document.getElementById('bigimage').src = $(this).find('img').attr('src');

or you have to get a jQuery object in order to call the .attr() method like:

jQuery('#bigimage').attr('src', $(this).find('img').attr('src'));
jAndy