Hide first image inside a div where the image is not a direct child

Viewed 2220

We are trying to hide all but the first image for each mydiv without hiding any of the text given the following html:

<div class="mydiv">
    <p>
        <img  src="http://placekitten.com/220/200" />
    </p>
    <p>text
        <img  src="http://placekitten.com/250/200" />
    </p>
    <p>
        <img src="http://placekitten.com/200/250" />
    </p>
    <h2>Text</h2>
</div>
<div class="mydiv">
    <p>
         text
    </p>
    <p>text
        <img src="http://placekitten.com/250/200" />
    </p>
    <p>
        <img  src="http://placekitten.com/200/250" />
    </p>
</div>

Here is an example on JSFiddle.

We would like to avoid altering the original html(because it is generated from a wiswig editor).
We cannot hide any of the text so hiding the entire paragraph is out of the question.

I basically want this functionality to work

div.mydiv p img ~ div.mydiv p img {
   display: none;
}

This does not work since the images are not direct siblings.

3 Answers

Using js:

document.getElementsByTagName("img")[0]
Related