ltibeachbum
ltibeachbum

Reputation: 13

jQuery append to src

I'm trying to refactor my existing code to now use jQuery and I can't figure out how to append my info to my src

Here was my existing line:

document.getElementById("itemDetail").src = detailUrl;

This is what I'm trying:

$("#itemDetail img src").append(detailUrl);

This is my snip of html:

<div id="detailsPane">
  <img src="images/blank-detail.jpg" width="346" height="153" id="itemDetail" />
</div>

Upvotes: 1

Views: 7820

Answers (4)

alessandrio
alessandrio

Reputation: 4370

this line:

document.getElementById("itemDetail").src = detailUrl;

in jQuery is:

$('#itemDetail').attr('src'...

you need

$('#itemDetail').attr('src',function(i,e){
    return e+=detailUrl;
})

Upvotes: 0

Pr3ds
Pr3ds

Reputation: 358

use attr

example:

$(this).attr("src", src);

you can see more in http://api.jquery.com/attr/

Upvotes: 3

Vandolph Reyes
Vandolph Reyes

Reputation: 620

You're almost there.

detailUrl = 'image.png';
$("#itemDetail").attr('src', detailUrl);

Upvotes: 1

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

First of all your selector should be $("img#itemDetail")

> If you need to set src you can use

$("img#itemDetail").attr('src' , detailUrl);

> If you need to append to the src you can use

$("img#itemDetail").attr('src' , $("img#itemDetail").attr('src') + detailUrl);

Upvotes: 2

Related Questions