dynamically create images only if they exist in the server

dynamically create images only if they exist in the server

hi i want to append dynamically <img> elements only if they exist in the server,
this is what i tried to do :  https://jsfiddle.net/85thcjfr/
what i expected is to get something like this :
<ul>
      <li><img src'=.../a/1.jpg'></li>
      .
      .
      .
      <li><img src'=.../a/7.jpg'></li>
<ul>
<ul>
      <li><img src'=.../b/1.jpg'></li>
      .
      .
      .
      <li><img src'=.../b/3.jpg'></li>
<ul>
thanks a lot for helping