Is there a way to select the value associated with .data ()?

Background: I am updating an existing photo gallery plugin that displays a large image in the center and gradually reduces the image on both sides.

So my question is this: I set some data on each image that gives the index of the image, the index of the next image and the index of the previous image, for example:

$(this).data('index', i).data('prev', i - 1).data('next', i + 1);

Later, I would like to be able to select an image based on its index. I tried the following but it does not work:

$('[data-index=' + index + ']');

Is there a way to select an item based on the piece of data attached with .data()?

+3
source share
2 answers

You can use .filter(), for example:

$('*').filter(function() { return $.data(this, 'index') == index; })

* , , , , .

+4

:

, HTML, - ?

$('#images img').eq(index)

+1

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


All Articles