Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select first child with jquery

Tags:

jquery

I am trying to select first child of ul and then remove it and then append it in the end of that ul but unfortunately i can not selet first child.

Here is code

current = 0;
$(document).ready(function(){

    width=951;

    var totalSlides=$(".slider ul li").length;
    $(".slider ul").removeAttr('width');
    $(".slider ul").attr('width',width*totalSlides);

    $('#next img').click(function(){
        current -= width;
        $(".slider ul").animate({"left":current+"px"}, "slow");
        $(".slider ul").append($(".slider ul:first-child"));
        //$('.slider ul:firstChild').remove();
    });
});
like image 488
mysterious Avatar asked Feb 03 '11 20:02

mysterious


People also ask

Is first child in jQuery?

It is a jQuery Selector used to select every element that is the first child of its parent. Return Value: It selects and returns the first child element of its parent.

How do you select the first paragraph in jQuery?

The :first selector selects the first element. Note: This selector can only select one single element. Use the :first-child selector to select more than one element (one for each parent). This is mostly used together with another selector to select the first element in a group (like in the example above).

How can I get second child in jQuery?

grab the second child: $(t). children(). eq(1);

What is nth child in jQuery?

jQuery | :nth-child() Selector jQuery :nth-child() Selector Selects all elements that are the nth-child of their parent. Syntax: $("Element:nth-child(Index/even/odd/equation)") Values: Index: Index provided. Index starts from.


2 Answers

You don't need to .remove()help or .detach()help a node to append it somewhere else. You can do it by just invoking .append()help or .appendTo()help respectively .after()help and .insertAfterhelp. For instance:

$('ul li:first').insertAfter('ul li:last');

Demo: http://www.jsfiddle.net/KFk4P/

like image 84
jAndy Avatar answered Oct 07 '22 02:10

jAndy


Any of these will work:

  • $('ul > li:first')
  • $('ul > li:first-child')
  • $('ul > li:eq(0)')
  • $('ul > li:nth-child(1)')
  • $('ul > li').eq(0)
  • $('ul > li').first()
  • $('ul > li')[0]
like image 22
zzzzBov Avatar answered Oct 07 '22 01:10

zzzzBov