javascript - Find an image tag? -


i have content editable div, have tags in it, eg.

<p>hello</p> <p>whatever <p>nested p bad idea can happen</p></p> <div>stuff</div> <p>test<img></p> 

for given element, need find img tag above in source order. image tag direct sibling, or child of sibling or parent of given element.

here's pretty simple recursive function should want , works generically.

function findpreceeding(element, targettype) {    var target = element.prev(targettype);    if (target.length > 0 || element.parent().length === 0)      return target;    else      return findpreceeding(element.parent(), targettype);  }    $(function() {    console.log(findpreceeding($("#div1"), "img").toarray());    console.log(findpreceeding($("#div2"), "img").toarray());    console.log(findpreceeding($("#div3"), "img").toarray());    console.log(findpreceeding($("#div4"), "img").toarray());  });
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <div>    <img id="img1" src="http://lorempixel.com/50/50/" />    <div id="div1">      <img id="img2" src="http://lorempixel.com/50/50/" />      <div id="div2">        <img id="img3" src="http://lorempixel.com/50/50/" />        <div id="div3">          <div id="div4">          </div>        </div>      </div>    </div>  </div>


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -