BlackQNX
BlackQNX

Reputation: 61

Reference in jQuery?

How can I change the code in jQuery which will be referred to the items above in the html?

Html code is as follows:

<div class="dramer">
    <ul>
        <li> </li>
        <li> <a class="shares" href="#"> Click </a> </li>
        <li> </li>
        <li> </li>
    </ul>

    <div class="drops"> </div>

</div>

The code in jQuery:

$('a.shares').click(function (event) {
    event.preventDefault();
    event.stopPropagation();
    $(this).next(."drops").slideToggle(400);    
});

Function in jQuery is designed to show and hide div located out of the reach <li> and <ul>

How can I modify the code to hide jQuery and show div?

Upvotes: 2

Views: 84

Answers (5)

Akhil Sekharan
Akhil Sekharan

Reputation: 12683

I'm not sure I understand what you what. If you mean you want to hide the div which is a child of your , you can try this:

$ ('a.shares'). click (function (event) {
    event.preventDefault ();
    event.stopPropagation ();
    $(".drops").slideToggle (400);
});

Upvotes: 0

Sampson
Sampson

Reputation: 268374

You should go up to the nearest ancestor that is shared between .shares and .drops, and then find .drops from there:

$(".shares").on("click", toggleDrops);

function toggleDrops (event) {
    $(this).closest(".dramer").find(".drops").slideToggle(400);
    return false;
}

Upvotes: 0

anderssonola
anderssonola

Reputation: 2195

Target it directly

$(".drops").slideToggle(400);

If you need it to be context aware, something like this might work

$(this).closest(".dramer").find(".drops").slideToggle(400);

Upvotes: 2

OpherV
OpherV

Reputation: 6937

How about trying

$ ('a.shares'). click (function (event) {
         event.preventDefault ();
         event.stopPropagation ();
         $(this).parent(".dramer").children(".drops").slideToggle(400);
     });      

Upvotes: 0

joequincy
joequincy

Reputation: 1395

$ (this). next(. "drops"). slideToggle (400);

$(this).closest('.dramer').children('.drops').slideToggle(400);

Upvotes: 1

Related Questions