I'm in a situation where I have three elements on one page.
<img class="imagem" />
<img class="imagem" />
<img class="imagem" />
I would like to select the second element of these 3 classes. How can I do this via jquery?
I'm in a situation where I have three elements on one page.
<img class="imagem" />
<img class="imagem" />
<img class="imagem" />
I would like to select the second element of these 3 classes. How can I do this via jquery?
Use the jQuery eq (indice) selector to select an element by its index.
var $elemento = $('.imagem:eq(1)');
See the example below by manipulating the element.
var $elemento = $('.imagem:eq(1)');
$elemento.attr('style', 'border:1px solid red');
.imagem {
width: 30px;
height: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><imgclass="imagem" />
<img class="imagem" />
<img class="imagem" />
Selecting by element index, you have the following options:
$('.imagem')[index];
$('.imagem').get(index);
$('.imagem:eq('+index+')');
Note: Remembering that the index starts from zero.