Javascript FAQ drop down

Posted by kdipaolo on Stack Overflow See other posts from Stack Overflow or by kdipaolo
Published on 2012-04-07T16:25:41Z Indexed on 2012/05/31 4:40 UTC
Read the original article Hit count: 166

Filed under:
|
|
|

Im trying to create a simple FAQ drop down but for some reason it is not working. Would you mind taking a look?

Thanks guys!

CSS

#faqs h3    { cursor:pointer; }
#faqs h3.active { color:#d74646; }
#faqs div { height:0; overflow:hidden; position:relative; }
#faqs div p { padding:0; margin-bottom:15px; } 

JavaScript:

$(document).ready(function() {
    $('#faqs h3').each(function() {
        var tis = $(this), 
            state = false, 
            answer = tis.next('div')
                        .hide()
                        .css('height','auto')
                        .slideUp();

            tis.click(function() {
                state = !state;
                answer.slideToggle(state);
                tis.toggleClass('active',state);
            });
    });
});

HTML

<div id="faqs">
    <h3>This is question 1?</h3>
        <div>
            <p>This is the answer to question #1.</p>
        </div>
    <h3>This is question 2?</h3>
        <div>
            <p>This is the answer to question #2.</p>
        </div>
</div>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html