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"})
)}
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>
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.
Do you get any errors on the console? Can you show us a screenshot of your project’s files, so we can see if the path is correct? Also, notice this would create man images, all with the same ID, which isn’t a good thing
@Sri Rama Punniya Vallabhar please approve any of the answers mentioned or comment if it doesn’t suffice your needs. This will help the community to identify the helpful one!
I don’t know their exact expectations since it wasn’t clearly mentioned. Simply intended to find the flaw in the existing code.