skip to Main Content

The images in the directory training_images/class1 are frame 0 to 99.

for (var i = 0; i < 100; ++i){                         
   $("#class1-images").prepend($('<img>',{id:'theImg',src:'training_images/class1/frame' + i + ".jpg"})                             
)}

So when I run this code, I do not see the images.

I need the images to be displayed.
I tried dragging the image into google chrome, and copied the path from there and pasted the URL in my code, but it’s still not working.

2

Answers


  1. You are trying to select a tag <img> which doesn’t exist. $ is a jQuery function equivalent to document.querySelector(). Therefore, to select the img tag you simply need to replace the <img>img. This is all what can be interpreted from the code provided by you!

    Final Code Compilation:

    for (var i = 0; i < 100; ++i){                         
       $("#class1-images").prepend($('img',{id:'theImg',src:'training_images/class1/frame' + i + ".jpg"})                             
    )}
    
    Login or Signup to reply.
  2. Your script should work – provided the target element exists and there are images behind the added URLs:

    for (var i = 0; i < 100; ++i){                         
       $("#class1-images").prepend($('<img>',{id:'theImg',src:`https://picsum.photos/id/${i}/200/150`})                             
    )}
    <script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
    <div id="class1-images"></div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search