Jquery how to select a specific element by index

Asked

Viewed 2,966 times

3

I would like to know how to select a specific element by the position of the index, because I can go 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 am 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();
    }
}
  • $(".ativo")[i]?

1 answer

5


There are two possibilities:

Example:

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

http://jsfiddle.net/Sfdpq/


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

For example:

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

http://jsfiddle.net/Sfdpq/1/


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

  • 2

    perfect worked properly, thank you very much for the help. $("#slide img:eq("+numero+")"). fadein(1000). addClass("element");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.