Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expanding / Collapsing divs

Tags:

jquery

php

I have multiple expanding / collapsing boxes on a single page being generated by PHP / MySQL.

Problem is, when I click on one link to expand a box, it expands all the boxes.

I thought about appending the post ID at the end of the class (<div class="postreplycontainer-POST_ID">) but I am not sure if that will work since I'd have to figure out a way to change the jQuery.

Here's a working example: http://jsfiddle.net/Draven/kUhkP/35/

Keep in mind, I can't manually code in each box because I am pulling the content from the database.

EDIT: Maybe somebody can help me with an additional problem.

I want to focus the textarea box when I expand the <div>. I tried using the same trick as before (using .closest but that didn't work).

Here's the example: http://jsfiddle.net/Draven/kUhkP/53/

This example will always focus the first <textarea>.

like image 963
Draven Avatar asked Jun 25 '13 07:06

Draven


People also ask

How can I expand and collapse a div using JavaScript?

In jQuery , the show() and hide() method are used to show or hide any element in HTML. We can use these functions to collapse or expand the div tag as shown below. Copy $('.

What is the difference between expand and collapse?

Expand takes the current member / element and exposes the elements in the next level down of the hierarchy - effectively showing itself and its 'children'. Collapse takes a currently expanded member / element and undoes the expanded mode, hiding all the exposes levels of the hierarchy beneath it.

How do you make a div collapse?

collapse class indicates a collapsible element (a <div> in our example); this is the content that will be shown or hidden with a click of a button. To control (show/hide) the collapsible content, add the data-toggle="collapse" attribute to an <a> or a <button> element.


1 Answers

Here's the FIDDLE

$("a.postreply").click(function () {
    $(this).closest('.blog-container')
        .find('.postreplycontainer').slideToggle("fast");
});
like image 163
Vond Ritz Avatar answered Oct 14 '22 01:10

Vond Ritz