Mark
Mark

Reputation: 704

Change image inside a div with class, id and other class qith Jquery

I have a div with this structure:

  <div class="post" id="post-160439">
       <a href="http://site blablabla" title="blablablabllba">
       <img width="240" height="180" src="myimage.jpg" 
            class="attachment-240x180 wp-post-    image" alt="my alt" title="my title"></a>
  </div>

And I want to change the image on hover, I have (I think) the most part done, but can't get the point to change ONLY this image, not all images, Like this;

    $('.wp-post-image').attr('src','my new image');

it changes all images, how can I tell jquery to change a divID.wp-post-image?

Thanks!!

Upvotes: 1

Views: 1744

Answers (3)

Adil Shaikh
Adil Shaikh

Reputation: 44740

As you have multiple div's with images, you can try this:

  $('div .post').each(function(){
     $(this).find('img').hover(function(){
       $(this).prop('src','my new image');
     },
     function(){
       //  callback
      }
      );
  });

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

Try

$('#' + varHere + ' .wp-post-image').attr('src','my new image');

Upvotes: 1

midudev
midudev

Reputation: 1061

A faster option:

$( document.getElementById( "post-160439" ) ).find( 'wp-post-image' ).attr('src','my new image');

Upvotes: 0

Related Questions