Are you trying to find a way to hide and show your content? The demo below shows a simple yet elegant way of toggling your content and toggling the control text via Javascript and styling.
Here is the sample HTML and Javascript code:
By default, the
peek-a-boo
text is loaded when the page loads but the display
attribute for the div that the content resides in is set to none
so it is not visible to the visitor. When the link is clicked, the toggle()
JavaScript functions executes and checks the value of the display style for the div that contains the content that we want to toggle.- If the display style is
none
, the function will: - Set the display style to
block
- This is executed in theelse
block of the function. The inner HTML content of a DOM element with ablock
display setting will be visible unless it is furthered controlled by CSS styling. - Change the link text to
hide
- The inner HTML of the link text, which in this case is justshow
, is replaced with thehide
text. - If the display style is
block
, the function will: - Set the display style to
none
- This is executed in theif
block of the function. The inner HTML content of a DOM element with thenone
display setting will not be visible for the viewer. - Change the link text to
show
- The inner HTML of the link text, which in this case is justhide
, is replaced with theshow
text.
Here is a more reusable and flexible
toggle
function that takes 2 parameters: one for the div to hide/show and a second parameter for the div that contains the link text to be switched.If you spice up this demo with some extra CSS styling, this can look like a nice little dialog box.
Here is the toogle2 JavaScript function:
As requested, here is an example of a JavaScript function that toggles multiple elements simultaneously. You can either toggle each DIV individually or use the button to toggle all 3 regardless of which toggle mode they are in.
This demo uses the toggle2 function as previously demonstrated and a new function called toggle3. I apologize for not being very creative on the function names. Anyway, here is the JavaScript code for toggle3:
Line 2 of the function checks to see if the first argument is an array or not. If it is an array, it will also assume that the second argument is an array as well. If it is an array, the script will loop through each element and execute toggle2 with each pair of elements in the arrays. Please note that this function also assumes that both arrays are in the same order such that contentDiv[3] and controlDiv[3] are a pair that refer to the same toggle element.
If the first argument is not an array, we will just pass the arguments as is to toggle2.
Here is the HTML code for the demo:
All the excitement is jammed into line 23 where we call the toggle3 function and pass over 2 arrays: one array containing all the content div ids and another array containing the header div ids. The rest is history =)
This demo was written in response to a request. We start off with some hidden divs and each click of the button will reveal one div at a time. When we have revealed all the divs, the button will disappear.
Here is the HTML code:
The HTML code contains 3 hidden divs to start off with. The button will launch the toggle4 JavaScript function and pass over the prefix of the div IDs. Each div id is named with the prefix box and a number following the name. For example, box1, box2, and box3. This is important for our JavaScript function. In addition, it increments the counter by 1 each time. This variable is initialized in our function.
Here is the JavaScript code:
Lines 1 and 2 will initialize two very important variables for us:
- counter - This variable will help us determine which box we will need to toggle.
- numBoxes - This variable represents the total number of boxes. This is important for us to know when we should hide the button.
Line 4 accesses the div we will need to toggle based on the name that is passed over as the argument and the counter. When these 2 values are concatenated, we get the name of the div we will need to toggle.
Lines 5-10 tells the same old story as before for toggling the content.
Lines 11-13 tests to see if we have reached our maximum number of divs to toggle. If so, it will access the toggle button and set the display attribute to none.
By popular demand, here is a demo that uses images instead of the Expand/Collapse text.
Here is the HTML code:
Everything is pretty much the same as before except the image tag is used instead of the Expand/Collapse text.
Here is the JavaScript code:
The toggle5 JavaScript function is pretty much the same as the rest of the toggle functions except that it switches the img tags instead of text.
Here is a new demo in response to a request where only one div is displayed at any one time.
Here is the plain HTML code:
Clicking on the links will execute the showonlyone JavaScript function and pass on the name of the div id.
Here is the showonlyone JavaScript code:
No comments:
Post a Comment