cjm2671
cjm2671

Reputation: 19496

What's wrong with this simple jQuery?

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

Answers (6)

Ed Fryed
Ed Fryed

Reputation: 2198

$("#logo img").attr("src","url here");

simplest way!!! dont need the .find.

Simples!!

Upvotes: 1

Pizzicato
Pizzicato

Reputation: 1621

Why not just do:

$('#logo>a>img').attr('src', 'http://www.domain.com/image.gif');

It's simpler and faster...

Upvotes: 0

Frenchi In LA
Frenchi In LA

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

amit_g
amit_g

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

Jishnu A P
Jishnu A P

Reputation: 14390

$('#logo a').find('img').attr('src', 'http://www.domain.com/image.gif'); 

this may work..

Upvotes: 1

Quintin Robinson
Quintin Robinson

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

Related Questions