img img

How to create an array from images in a div

<div id="w01">
<img src=g_bur/01.jpg alt='img'>
<img src=g_bur/02.jpg alt='img'>
<img src=g_bur/03.jpg alt='img'>
</div>

I need an array of elements from these images

Js

var arr = [];
$("#w01 > img").each(function(){
    arr.push($(this));
}
alert (arr) //error

Error: Uncaught SyntaxError: Unexpected identifier

+4
source share
2 answers

Use toArray():

var arr = $("#w01 > img").toArray();

... or modify the source code as follows:

var arr = [];
$("#w01 > img").each(function(){
    arr.push(this); // this instead of $(this) so you only get the <img>
}); // you were missing a closing paren
alert(arr);
+3
source

You forgot to put ')'

Here:

$("#w01 > img").each(function(){
    arr.push($(this));
}); //<<<<
+1
source

Source: https://habr.com/ru/post/1540078/


All Articles