Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JQuery to select parent element of "this" (element clicked)

Tags:

I have a jQuery script that creates a carousel to rotate images left and right on user click. At first, I wasnt planning on putting more than one carousel on a single page, but now the need has arrived.

The problem is, I dont know how to refer to one carousel (the one clicked) when the user clicks a button.

Heres the script

$(function()
{
    // Put last item before first item, in case user clicks left
    $('.carousel li:first').before($('.carousel li:last'));

    // Right click handler
    $('.right-button img').click(function()
    {
        // Get width plus margins
        var item_width = $('.carousel li').outerWidth() + (2 * parseInt($('.carousel li').css('margin-right')));

        // Get left indent
        var orig_left_indent = $('.carousel').css('left');

        // Calculate new left indent
        var left_indent = parseInt(orig_left_indent) - item_width;

        $('.carousel:not(:animated)').animate({'left': left_indent}, 500, 'swing', function()
        {
            // Put first item after last item
            $('.carousel li:last').after($('.carousel li:first'));

            // Set left indent to default
            $('.carousel').css({'left': orig_left_indent});
        });
    });

    // Left click handler
    $('.left-button img').click(function()
    {
        // Get width plus margins
        var item_width = $('.carousel li').outerWidth() + (2 * parseInt($('.carousel li').css('margin-right')));

        // Get left indent
        var orig_left_indent = $('.carousel').css('left');

        // Calculate new left indent
        var left_indent = parseInt(orig_left_indent) + item_width;

        $('.carousel:not(:animated)').animate({'left': left_indent}, 500, 'swing', function()
        {
            // Put last item before first item
            $('.carousel li:first').before($('.carousel li:last'));

            // Set left indent to default
            $('.carousel').css({'left': orig_left_indent});
        });
    });

    // Close button handler
    $('.carousel-container .close-button img').click(function()
    {
        $('.carousel-container').fadeOut(1000);
    });
});

As of now, when you click right or left on any carousel, all of them shift. I dont know how to get a reference to the carousel clicked.

heres the HTML

<div class="carousel-container clearfix">
    <div class="header close-button">
        <strong>Check These Out</strong>
        <?php echo html::image('media/images/close.gif'); ?></div>
    <div class="left-button"><?php echo html::image('media/images/left_arrow.png'); ?></div>
        <div class="carousel-inner">
            <ul class="carousel">
                <?php foreach ($items as $item): ?>
                <li> ... </li>
                <?php endforeach; ?>
            </ul>
        </div>
    <div class="right-button"><?php echo html::image('media/images/right_arrow.png'); ?></div>
</div>

How would I achieve this, I dont know how to reference the carousel that the user clicked, because the arrows are child elements of the carousel.

EDIT: Thanks for the answers. carousel = $(this).parent() works, but how do I check if the carousel is :animated using the selector and the new carousel variable?

$(':animated', carousel) ?

like image 223
BDuelz Avatar asked Jun 25 '10 15:06

BDuelz


People also ask

How do I find a specific parent in jQuery?

The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.

Which jQuery method returns the direct parent element of the selected element?

jQuery parent() Method The parent() method returns the direct parent element of the selected element.


2 Answers

Inside of an event handler, the variable:

$(this)

will get you the calling element. From there, to get the containing div you can use the parent() function:

$(this).parent()

Use that to walk through the DOM.

like image 74
riwalk Avatar answered Oct 18 '22 11:10

riwalk


Use the .parent() function go up a level. Your code might look something like this:

$('.right-button img').click(function()
    {
        carousel = $(this).parent();

        //bunch of code
    }
like image 38
Peter Anselmo Avatar answered Oct 18 '22 10:10

Peter Anselmo