Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-10-23 13:30:27

Gallex
Member
Registered: 2006-10-08
Posts: 1,308

jquery toggle

then the h3 element is clicked, show the content inside the div element below it. then the button element inside that div is clicked, hide the div. and here is code i’m using:

$("h3.hide, button.hide").click(function () {
        $("div.hide").toggle("slow");
});    

everything working correctly until there is only one h3 and div element with hide class. but then there is more than one h3 and div element with hide class, it shows all the divs with hide class then the first h3 element is clicked. but i have more than one and i need them to show/hide one-by-one. can you help me?

Last edited by Gallex (2013-10-23 13:49:38)

Offline

#2 2013-10-23 13:41:43

etc
Developer
Registered: 2010-11-11
Posts: 5,195
Website GitHub

Re: jquery toggle

Try this:

$("h3.hide").click(function () {
        $(this).next("div.hide").toggle("slow");
});

$("button.hide").click(function () {
        $(this).parents("div.hide").toggle("slow");
});

Offline

#3 2013-10-23 13:50:27

Gallex
Member
Registered: 2006-10-08
Posts: 1,308

Re: jquery toggle

brilliant! thank you oleg!

Last edited by Gallex (2013-10-23 13:50:59)

Offline

#4 2013-10-23 15:55:13

etc
Developer
Registered: 2010-11-11
Posts: 5,195
Website GitHub

Re: jquery toggle

Fine, but I would rather do it this way: remove div.hide { display:none } CSS rule, and add

$("div.hide").hide();

Otherwise js-disabled users won’t be able to see your content, unless they disable css too.

Offline

#5 2013-10-24 06:34:10

Gallex
Member
Registered: 2006-10-08
Posts: 1,308

Re: jquery toggle

like this:

$("h3.hide").click(function () {
        $(this).next("div.hide").toggle("slow");
$("div.hide").hide();
});

$("button.hide").click(function () {
        $(this).parents("div.hide").toggle("slow");
});

Offline

#6 2013-10-24 17:29:23

etc
Developer
Registered: 2010-11-11
Posts: 5,195
Website GitHub

Re: jquery toggle

Not exactly:

$("div.hide").hide();

$("h3.hide").click(function () {
        $(this).next("div.hide").toggle("slow");
});

$("button.hide").click(function () {
        $(this).parents("div.hide").toggle("slow");
});

Offline

#7 2013-12-16 13:30:42

Gallex
Member
Registered: 2006-10-08
Posts: 1,308

Re: jquery toggle

very similar situation, but stuck again – doesn’t open a div.

my goal: span element with hide class inside paragraph should open the div below.

<p>Eesti Loodusuurijate Seltsi ornitoloogiasektsiooni esimehe Heinrich Veromanni kirjutatud <span class="hide">avanumbri saatesõna.</span></p>

<div class="hide">

<p>Palju aastaid puudus Eesti Loodusuurijate Seltsi ornitoloogiasektsiooni juhatusel võimalus enam-vähem korrapärase info saatmiseks loodusvaatlejatele.</p>

<button class="hide">Close</button>
</div>

my code:

$("div.hide").hide();

$("span.hide").click(function () {
        $(this).next("div.hide").toggle("slow");
});

$("button.hide").click(function () {
        $(this).parents("div.hide").toggle("slow");
});

Offline

#8 2013-12-16 14:22:22

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,091
Website GitHub Mastodon Twitter

Re: jquery toggle

Hi Gallex should that be show in your code? I see slow there.

Also maybe this will be of help


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#9 2013-12-16 14:48:48

Gallex
Member
Registered: 2006-10-08
Posts: 1,308

Re: jquery toggle

no, because oleg’s previous code for h3 element works well

Offline

#10 2013-12-16 16:14:50

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,306

Re: jquery toggle

Gallex wrote:

$("span.hide") […]

Don’t see that in your HTML.

Last edited by uli (2013-12-16 16:15:39)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#11 2013-12-16 16:28:31

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: jquery toggle

…and even if there is, its searching div.hide node from the wrong place. Its not a sibling of the span, one up in the tree. So, you must climb or change selecting method. This:

$(this).next("div.hide").toggle("slow");

Would become:

$(this).parent().next("div.hide").toggle("slow");

Offline

#12 2013-12-17 08:47:51

Gallex
Member
Registered: 2006-10-08
Posts: 1,308

Re: jquery toggle

thank’s jukka

Offline

Board footer

Powered by FluxBB