thedonlee
thedonlee

Reputation: 25

How to change image src using jQuery

<div class="branding">
<h1 class="logo">
<a href="http://www.whatever.com/" class="logo">
<img src="http://www.whatever.com/test.png"></a>
</h1>
</div>

I am trying to access the image src with the following to no avail:

$("a.logo img").html("<img src='http://www.whatever.com/newimage.png'>");

The reason I am doing this is because the image is on a third party that I can't access the codebase to make the change so I am trying to rip out their with my code.

Upvotes: 0

Views: 97

Answers (4)

thedonlee
thedonlee

Reputation: 25

I figured out the answer:

$('a.logo img').attr("src", "http://www.whatever.com/newimage.png" );

Thanks!

Upvotes: -2

oligan
oligan

Reputation: 634

Did you try

img.attr('src','http://mynewsource.com');

Upvotes: -1

Marc B
Marc B

Reputation: 360812

.html() changes the html CONTENT of a node, e.g.

orig: <p>foo</p>
$('p').html('bar');
neW: <p>bar</p>

An <img> has NO content. Try

$('p.logo img').attr('src', 'new url here');

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82251

src is attribute. You should use .attr() to get/set attribute values using jquery:

 $("a.logo img").attr("src","http://www.whatever.com/newimage.png");

Upvotes: 3

Related Questions