Reputation: 19496
I'm targetting the image in the following HTML
<h1 class="logo-site-name">
<span id="logo">
<a href="http://some.link">
<img src="http://www.other.com/some.gif">
</a>
</span>
</h1>
And I'm attempting to replace the image with the following code, but it isn't working. Am I selecting & modifying correctly?
$('#logo').children('img').attr('src', 'http://www.domain.com/image.gif');
Upvotes: 0
Views: 82
Reputation: 2198
$("#logo img").attr("src","url here");
simplest way!!! dont need the .find.
Simples!!
Upvotes: 1
Reputation: 1621
Why not just do:
$('#logo>a>img').attr('src', 'http://www.domain.com/image.gif');
It's simpler and faster...
Upvotes: 0
Reputation: 3169
You can also use context. Usually is cleaner, and you will limit your selection. something like:
$('a', '#logo').attr('src', 'http://www.domain.com/image.gif');
Upvotes: 0
Reputation: 31270
Children looks for only immediate children (one level only) .children() . Use find
$('#logo').find('img').attr('src', 'http://www.domain.com/image.gif');
Upvotes: 0
Reputation: 14390
$('#logo a').find('img').attr('src', 'http://www.domain.com/image.gif');
this may work..
Upvotes: 1
Reputation: 82375
Your img
element isn't a child of logo
, it is a descendant.
You could do
$('#logo').find('img:first').attr('src', 'http://www.domain.com/image.gif');
The first
is optional, but if you only have one I like to restrict.
Upvotes: 4