Dennis
Dennis

Reputation: 107

Is there a way to set a background image on an img tag that doesn't have an id in jquery?

I have an image within a header 3 tag and I have a jquery function that expands and collapses the table. I need to change the expand / collapse icon when the function is done, which I can do. My question is can I change the image that is within the h3 tag even if it doesn't have an id?

So something like:

$(this.img).css("backgroundImage", "url(images/expand.png)");

Upvotes: 1

Views: 291

Answers (5)

samccone
samccone

Reputation: 10926

Sure,

$(this).animate({'height':'toggle'},function(){$(this).find('img').attr('src','pathtoimage.png');})

Upvotes: 0

Damb
Damb

Reputation: 14600

Sure..

$('h3 img').css(/* --- */);

Just don't use .img because that is for a css class called "img".

Upvotes: 0

Soatl
Soatl

Reputation: 10592

Yes it is possible.You have a typo. backgroundImage should be background-image like this:

$(this.img).css("background-image", "url(images/expand.png)");

Upvotes: 0

Adam Bergmark
Adam Bergmark

Reputation: 7546

Why are you setting the CSS instead of changing the src of the image? Try $("img", this).attr("src", "images/expand.png");

Upvotes: 0

ChrisThompson
ChrisThompson

Reputation: 2008

$('h3').find('img').attr('src', 'url(images/expand.png)')

Or since you have a reference within the toggle:

$(this).find('img').attr('src', 'url(images/expand.png)')

Upvotes: 1

Related Questions