Learning Outcomes
On completion of this section you will know:
- The difference between display:block and display:none
- How to make selected parts of a page collapse or appear
- How to alter styling using JavaScript
On completion of this section you will know:
Typing the phrase 'interactive web pages' into a Google search will give you a set of options as shown below in 'Collapsed List'
As well as links to possible useful web sites the search also provides other suggestions of potentially useful sidelines under the heading 'People also ask:'. To the right of each of the lines is a V shaped icon which signals that each of those lines can be further expanded.
Clicking on the v-icon of the first line displays the hidden content as shown in 'Expanded List Item' below.
Clicking on the same v-icon would collapse the list back to its original shape.
In this lesson we shall look at creating a similar effect while using a more ornate display.
Go to topInside the <main>, apart from the <h2> heading, the rest of the block consists of pairs of <h3> and <article> elements.
The <h3> elements will always stay on the screen but the display of the <article> elements can be turned on or off by clicking on the heading above them.
Now let us see how this works as far as the HTML code is concerned.
The first <h3>/<article> pair spans lines 12 to 22.
At line 12 an <h3> element is marked up with a value of 'Ben Johnson'. It also has an event trigger which launches the function changeView() if the user clicks on the heading. The function is passed the parameter string 'Johnson'. In a moment we shall see the significance of this.
Line 13 starts the <article> block starts. It is given an id of 'Johnson'. This complements the 'Johnson' that is passed as a parameter to changeView() at line 12.
The function changeView() acts as a toggle switch for turning on or off the visibility of any of the <article> blocks on this page. When it is called it is passed the id value of the <article> block that is to be processsed. This block is then tested for being visible. If it is then it is rendered invisible and vice versa.
All other <article> pairs on the page are processed in the exactt same manner and so there is no need to discuss them.
We shall now look at the JavaScript code to see how the visibility of the <article> pairs are effected.
Go to topLines 1 and 2 of Listing 3 declare and populate two variables. The variable varStyleOn is given the value 'display:block:visibility:visible'. This looks like a piece of CSS, which in fact it is. The only difference to regular CSS is that this will be applied spontaneously to an element by a JavaScript command. As you can see it will make visible whichever HTML element it is applied to.
The second variable, varStyleOff if given a value that will turn off the visibility of whichever element it is applied to
Line 3 is the actual start of the function. If it had been called from line 12 of Listing 1 then the parameter itemID would have a value of 'Johnson'.
In line 5 the document is searched for the element whose value is 'Johnson'. If found than a pointer to that element is stored in the variable elem.
With a pointer to our element now stored in the variable elem we cab now do any processing on that element we wish. All we want to do however is to find out whether it is visible or hidden, and whichever state of visibility it is in change it to the other state.
To do this we first need to know what visibility state it is in currently. For this we go to line 6. This line is more easily explained by using an English sentence. This could be phrased as 'find the visibility property of elem and store it in the variable varVisible
The rest of the code is very simple: if varVisible has a value of 'visible' then at line 8 the element pointed to by elem has the styling of varStyleOff applied to it, which means that it is rendered invisible.
On the other hand if varVisible has a value other than 'visible' then line 10 is activated. This applies the styling specified in varStyleOn to elem, thus making it visible.
Go to topThe above CSS file is shown for completion and in case you want to use the code to create your own version of our sample web page.
Go to top