Jquery how to select a specific element by index

3

I would like to know how to select a specific element by the position of the index, as I can move through the structure with the next and prev functions, but I'm not finding a way to select an element directly by its position. I'm using Jquery

function proximo()
{
    if($(".elemento").next().size())
    {                              
       $(".elemento").fadeOut().removeClass("elemento").next().
           fadeIn(1000).addClass("elemento");              

      var texto = $(".elemento").attr("alt");
      $("#slide p").hide().html(texto).delay(500).fadeIn();
    }
}
    
asked by anonymous 03.07.2014 / 16:27

1 answer

5

There are two possibilities:

Example:

$('li:eq(1)').css('color', 'blue');
$('li').eq(2).css('color', 'red');

link

If you already have a collection of elements and you want to choose one of them through the index you can use .index(numero)

For example:

$('li').click(function(){
   this.innerHTML = $(this).index(); // vai mudar o texto para o numero do index
});

link

You can always use simple javascript, taking into account that a collection is array type, using $(variosElementos)[numero] . But be aware that this way returns you a raw object (I want to translate to "crú" ...), but in the background an object without the jQuery methods added.

    
03.07.2014 / 16:31