Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Not sure which method to use, closest() and parent() don't work

God i feel like i'm spamming stackoverflow, this is my 3rd post for today. Sorry, heh.

I even posted a question regarding this before, kind of, but i've changed the code a bit since so i thought it was better to post a new question.

$('.pmlist ul li h4 .toggle').click(function() {
    $(this).closest('.meddel').toggle(250);
});

That's what i've got now. The reason why the closest() method isn't working is because the div .meddel is just next to the h4 element. And closest() only crawls right up the DOM tree, ignoring other child elements. Right? parent() works almost the same and doesn't work either.

And as i only want to toggle the closest .meddel div in the element, i need something that, yeah justs grabs the nearest one, and not all of them.

To clear it up a bit, here's the HTML for one list item:

<li class="item">

<h4><a class="toggle">[topic]</a><small>2010-04-17 kl 12:54 by <u>[name]</u></small></h4>

<div class="meddel">
    <span>
        <img style="max-width: 70%; min-height: 70%;" src="profile-images/img.jpg" alt="" />
        <a href="account.php?usr=47">[name]</a>
    </span>

    <p>text</p>
</div>

</li>

I have several items like that, and if i click one toggle link, i just want the nearest .meddel to be toggled, as mentioned before.

Thanks.

like image 293
qwerty Avatar asked Apr 18 '10 21:04

qwerty


People also ask

What is the difference between parent and closest in jQuery?

Given a jQuery object that represents a set of DOM elements, the .closest () method searches through these elements and their ancestors in the DOM tree and constructs a new jQuery object from the matching elements. The .parents () and .closest () methods are similar in that they both traverse up the DOM tree.

What is the parent method in jQuery?

jQuery | parent () & parents () with Examples Last Updated : 13 Feb, 2019 The parent () is an inbuilt method in jQuery which is used to find the parent element related to the selected element. This parent () method in jQuery traverse a single level up the selected element and return that element.

What is the closest method in jQuery?

Given a jQuery object that represents a set of DOM elements, the .closest() method searches through these elements and their ancestors in the DOM tree and constructs a new jQuery object from the matching elements.

What is the use of parentsuntil in jQuery?

jQuery parentsUntil () method The parentsUntil () method is an inbuilt jQuery method. It is used to get all ancestor elements between the selector and stop. This method traverses upwards from the parent element along with the ancestors.


1 Answers

You could just use:

$(this).parent().next().toggle(250);

Update (according to tvanfosson comment):

$(this).parents(".item").find(".meddel").toggle(250);

This is a much more flexible and safer solution. First it gets the entry container (.item), then find the body of the entry and toggles it. It's less sensitive to feature changes in DOM.

like image 141
Crozin Avatar answered Dec 08 '22 01:12

Crozin