Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find parent and first child of it's parent

I am trying to find the parent of an element and it's parent first child, the code is like this:

        <ul class="lowerMenu">
            <li><a href="" class="selected">Welcome</a></li>
            <li><a href="">Logo</a></li>
            <li><a href="">Websites</a></li>
            <li><a href="">Stationery</a></li>
            <li><a href="">Illustration</a></li>
            <li><a href="">Full Pack</a></li>
        </ul>

function setIntervalAndExecute() {
    changeImages();
    setTimeout(function(){
        showImages(imagesArray);
    },500);
    var intervalot = window.setInterval(function(){
        changeImages();
        var selected = $('.selected');
        if (!selected.parent().is(':last-child')) {
            $('.selected').parent().next().children().addClass('selected');
            selected.removeClass('selected');
        } else {
            $('.selected').parent().parent().children(':first-child').addClass('selected');
            selected.removeClass('selected'); // nesho ne mi rabotit ovdeki
        }
        window.setTimeout(function(){
            showImages(imagesArray);
        },500);
    },10000);
    return intervalot;
}
var intervalot = setIntervalAndExecute();

I know it's a little complicated but i'm new to jquery , so what i'm trying to do is after the class 'selected' gets to the last element i want to remove it and set it to the first element. I've tried with this but it doesn't seem to be working

$('.selected').parent().parent().children(':first-child').addClass('selected');

and when it gets to the last element the interval is executing twice and then stops. This is the site that i'm working on:

http://nikodola.com/testsite

like image 201
pashata Avatar asked Aug 30 '13 10:08

pashata


2 Answers

If you go up two levels, then find the next child, you're only going down one level -- you're setting the selected class on the <li>, not the <a> below it.. You need to go down another level. So it should be:

$('.selected').parent().parent().children(':first-child').children().addClass('selected');

Another way to get there is:

$('.selected').parent().siblings(':first-child').children().addClass('selected');
like image 84
Barmar Avatar answered Sep 28 '22 17:09

Barmar


$('.selected').parent().siblings(':first-child').children().addClass('selected');

FIDDLE DEMO

like image 30
RONE Avatar answered Sep 28 '22 16:09

RONE