1

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.

2 Answers 2

1
$('.elastica').click(function(){
    $('#bigimage').attr('src', $(this).attr('src'));
});
Sign up to request clarification or add additional context in comments.

6 Comments

you should go jQuery all the way: $('#bigimage').attr('src', $(this).attr('src'));
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');
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: 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.
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.
|
0

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'));

1 Comment

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');

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.