StudioTime
StudioTime

Reputation: 23979

Change css attributes of previous class

I have the following html structure:

<div class="hold_image">
    <div class="SiteName">guitars.com</div>
    <a href="/image/Guitars/1236">
        <img src="//mysite.com/images/production/1236.jpg" class="Image">
    </a>
</div>

I want to change the parent outer div (class="hold_image") to have a different color background when the image loads and am trying:

$(".Image").on('load', function () {
    $(this).prev('.hold_image').css('background-color','#ffffff');
});

But nothing changes. Is there a better way to do this?

Upvotes: 0

Views: 37

Answers (2)

timing
timing

Reputation: 6455

Hi instead of prev you want to use closest('.hold_image'). This selector searches up the dom tree until it has a match.

Upvotes: 1

Jack
Jack

Reputation: 2830

Instead of .prev(), you want to use .parents().eq(1).

Upvotes: 0

Related Questions