1

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>
2
  • $("img#itemDetail").attr('src' , detailUrl); Commented May 3, 2017 at 13:55
  • append or set/replace src (fomr the look of your original js, you are replacing)? Commented May 3, 2017 at 13:56

4 Answers 4

3

use attr

example:

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is how to replace, not how to append to "src".
2

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

Comments

1

You're almost there.

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

Comments

0

this line:

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

in jQuery is:

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

you need

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.