Good code must be well structured, and this case is no exception. Coming up with a plan and method of attack is essential to developing a page that will be logical and easy to maintain with future modifications. Forward thinking can help a developer from writing themselves into a corner and having to completely rework their pages when things need to be adjusted. One of the best ways to pattern code is simply by extracting as much of the JavaScript from the markup as possible. This allows the pages to be rendered and styled even when JavaScript is disabled - as in the case of screen readers or old browsers.
By allowing for your pages to be displayed properly when JavaScript is not present, you are allowing for accessibility and expansion as your site grows. For this article, we will look at a very well defined structure for our markup followed by precise targeting of our selected elements through JavaScript to show and hide them as needed. To target individual elements with JavaScript, there must be reference points accessible, so we must be sure to appropriately assign attributes to the elements in a manageable way. Keep in mind, too, that we want to be able to select individual elements without scanning the entire document, if possible - thus helping our page load time.
You can see the structure of the markup we will be using in this article here:
FAQ Markup Sample
As it stands, this markup will display well formed FAQs, especially with a bit of CSS applied to make it pop visually. This is precisely what we want to achieve. Now, let's look at how to add our interaction with as minimal a footprint as possible while still allowing for scalability.