0
<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.

Brewal
  • 8,067
  • 2
  • 24
  • 37
thedonlee
  • 25
  • 3
  • 1
    possible duplicate of [Changing the image source using jQuery](http://stackoverflow.com/questions/554273/changing-the-image-source-using-jquery) – Dan Sep 16 '14 at 15:13
  • don't you google first ?? – A.T. Sep 16 '14 at 15:17

4 Answers4

3

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");
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1

.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');
Marc B
  • 356,200
  • 43
  • 426
  • 500
-1

Did you try

img.attr('src','http://mynewsource.com');
oligan
  • 634
  • 6
  • 18
  • Thank you but it appears that this only works on the latest jQuery. The version I am relegated on using is jquery-1.7.2.min.js which does update the image but then some of the other jQuery elements on page no longer work. Strange. – thedonlee Sep 16 '14 at 15:59
  • well ok ,if you want more help on this give some more details in your question. – oligan Sep 17 '14 at 07:47
-2

I figured out the answer:

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

Thanks!

MH2K9
  • 11,951
  • 7
  • 32
  • 49
thedonlee
  • 25
  • 3