Search code examples
javascriptjqueryhtmlgreasemonkeytampermonkey

Javascript - Find IMG source and change DIV background


I have found many examples for different circumstances, but im just not experienced enough to combine them and accomplish the result I want. I am trying to create a TamperMonkey/GreaseMonkey script to highlight a DIV that matches a particular image source.

The website code is as follows, I cannot change it, but I removed the URL for the image and excess code so its easier to read:

<div class="listing_results>
    <div class="listing_row" id="listing_1">
        <div class="listing_img_container">     
            <img id="listing_1_image" src="image-source-1.jpg">
        </div>
    </div>
    <div class="listing_row" id="listing_2">
        <div class="listing_img_container">     
            <img id="listing_2_image" src="image-source-2.jpg">
        </div>
    </div>
    <div class="listing_row" id="listing_3">
        <div class="listing_img_container">     
            <img id="listing_3_image" src="image-source-3.jpg">
        </div>
    </div>
</div>

Im looking for code to search for image-source-#, and change the background color of the listing-row DIV when I find it (for example, search for image-source-2.jpg and change listing_row to green).


Solution

  • $('img[src="image-source-2.jpg"]').parent().css('background-color','red');
       div{
       	  padding:5px;
       }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    
    <div class="listing_results>
        <div class="listing_row" id="listing_1">
            <div class="listing_img_container">
                <img id="listing_1_image" src="image-source-1.jpg">
            </div>
        </div>
        <div class="listing_row" id="listing_2">
            <div class="listing_img_container">
                <img id="listing_2_image" src="image-source-2.jpg">
            </div>
        </div>
        <div class="listing_row" id="listing_3">
            <div class="listing_img_container">
                <img id="listing_3_image" src="image-source-3.jpg">
            </div>
        </div>
    </div>