Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using JavaScript to close the answer container

From: Create an Expandable FAQ Listing with jQuery and Dreamweaver

Video: Using JavaScript to close the answer container

Now to begin writing our JavaScript to open and close the answer based on clicking the question, let's switch over to faq.js. Inside up here we're going to start by using jQuery's document ready statement. So let's start by typing a dollar sign, beginning parenthesis. Now if you're using Dreamweaver CS5.5, there's built-in code completion for jQuery, so you'll notice I can see the word "document" in this, and I can use my arrow keys to select from the individual items. If you're not using Dreamweaver CS5.5, you can still type in all of the JavaScript statements, but you'll just have to type them in by hand, and I'll be doing that throughout the course as well.

Using JavaScript to close the answer container

Now to begin writing our JavaScript to open and close the answer based on clicking the question, let's switch over to faq.js. Inside up here we're going to start by using jQuery's document ready statement. So let's start by typing a dollar sign, beginning parenthesis. Now if you're using Dreamweaver CS5.5, there's built-in code completion for jQuery, so you'll notice I can see the word "document" in this, and I can use my arrow keys to select from the individual items. If you're not using Dreamweaver CS5.5, you can still type in all of the JavaScript statements, but you'll just have to type them in by hand, and I'll be doing that throughout the course as well.

So let's start by typing document, inside of the parentheses, end the parentheses. Outside of the parentheses, type .ready, beginning and ending parentheses, semicolon, inside of the parentheses for ready, type function, beginning and ending parentheses, beginning and ending brackets. Let's split that open on the brackets. So inside of this document ready is basically everything that's going to execute once all of HTML and CSS code has been loaded into the browser's Document Object Model, or the DOM. So once we know everything is there, let's start by typing dollar sign, beginning and ending parentheses, inside two single tick marks for a string literal, .faq_question.

We're going to search for every question item in the page. Outside of the parentheses .click, beginning and ending parentheses, semicolon, inside of the parentheses for click, type function, parentheses and brackets. Let's split that open on the brackets. So for every item that has the faq_ question class assigned to it that's clicked on, inside up here we're going to type if, beginning and ending parentheses, beginning and ending brackets, split that open.

Inside of the parentheses is the condition we're going to check for. I'll put a few spaces in here, type dollar sign, beginning and ending parentheses, type this inside of the parentheses. This refers to the actual item that was clicked .parent, beginning and ending parentheses, after the parentheses type .is beginning and ending parentheses, inside of the is, two tick marks for a string literal, .open. So what we're doing here is we're checking to see if the item we clicked on; if its parent container has a class called open assigned to it.

This way, we can check whether or not the answer is actually in an open state or not. So inside of the if, let's type dollar sign, parentheses this. That's out of the parentheses .closest, beginning and ending parentheses. Inside of closest, we're going to type two tick marks for a string literal, .faq, outside of the parentheses .find, beginning and ending parentheses.

Inside of find two tick marks for string literal, .faq_answer_container. Outside of the parentheses we're going type .animate, beginning and ending parentheses, semicolon. Inside of animate, inside of the parentheses for animate, beginning and ending brackets, inside of the brackets two tick marks for a string literal. We're going to set the height that's out of the string literal, colon, two tick marks for another string literal, and type 0, outside of the brackets but still inside of the parentheses for animate, comma 500 milliseconds or half a second.

Next line, dollar sign, parentheses, this inside of the parentheses .closest, beginning and ending parentheses. Inside of the parentheses tick marks for a string literal, .faq, outside of the parentheses .removeClass, beginning and ending parentheses, semicolon. Inside of removeClass, two tick marks for a string literal, type in the word "open." So what the script is doing is it's checking the parent of the item that was clicked to see if the open class has been assigned.

This way we know whether or not the answer's in an open or closed state. And if it is in an open state we're going to go through and find the corresponding answer container, and we're going to animate it back to 0, and then we're going to take the open class off of that object. Now we have half of our script in place, in the next movie we'll put an else statement in that will open the answer if it's not in an open state, and this will give us that toggling effect for opening and closing the answer div.

Show transcript

This video is part of

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Create an Expandable FAQ Listing with jQuery and Dreamweaver.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.