Developer

Stupid Web Tricks: Dynamic content

Dynamic content is useful in many places, like questionnaires. Learn to build dynamic content that bases future questions on past answers.

Click here for our complete list of Stupid Web Tricks.

One of the goofiest things about Web-based questionnaires is that, on most of them, the questions are totally unrelated to one another. One example of this goofiness is medical questionnaires—how can they ask me my gender on question three and then on line twenty-three, ask if I'm pregnant or might be pregnant? What these questionnaires need is a way to base future questions on past results.

The solution that I came up with uses a combination of Cascading Style Sheets, JavaScript, and the HTML DOM. There is a style property called display, which can be used to change the style display property of an HTML element. Set the display property to None and the element disappears, leaving only a memory; set the property to Inline, and it's back.

The example in Listing A illustrates some of the ways that the display property can be used to avoid those embarrassing questions. Elements are displayed, or not, based upon the client's input.

Click here to see it in action.

Editor's Picks