| | |
Set visibility of div tags
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Hi,
this is my problem:
1. There are two div elements in my page.
2. When the page loads only one is supposed to be visible.(This is where i'm getting stuck)
3. There are a series of checkboxes.
4. On clicking on one particular checkbox the first div element must disappear and the other div element must be made visible.
I'm able to make one div element appear and disappear but both appear when the page loads.
Please help.
this is my problem:
1. There are two div elements in my page.
2. When the page loads only one is supposed to be visible.(This is where i'm getting stuck)
3. There are a series of checkboxes.
4. On clicking on one particular checkbox the first div element must disappear and the other div element must be made visible.
I'm able to make one div element appear and disappear but both appear when the page loads.
Please help.
A simple demo that will prevent div2 of showing itself if div1 is currently visible on the page:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<head> <script type="text/javascript"> <!-- var showDiv = function() { var div1 = document.getElementById("div1"); var div2 = document.getElementById("div2"); if ( div1.style.display === "none" ) { div2.style.display = "block"; return; } div2.style.display = "none"; }; window.onload = showDiv; // --> </script> </head> <body>...
•
•
•
•
A simple demo that will prevent div2 of showing itself if div1 is currently visible on the page:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<head> <script type="text/javascript"> <!-- var showDiv = function() { var div1 = document.getElementById("div1"); var div2 = document.getElementById("div2"); if ( div1.style.display === "none" ) { div2.style.display = "block"; return; } div2.style.display = "none"; }; window.onload = showDiv; // --> </script> </head> <body>...
My page has a lot of elements, so when the page loads you can see all the items for a second before they disappear because of the code.
Is there anyway property which sets their visibility to false when it loads itself so we cant see the unwanted items?
I won't be able to provide you example using getters and setters under my OPERA browser. But here's a little program that allows you to set display properties from any elements' inside your page:
hope it helps...
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Style-Type" content="text/css"> <meta http-equiv="Content-Script-Type" content="text/javascript"> <meta http-equiv="Window-target" content="_top"> <title>Free Live Help!</title> <style type="text/css"> <!-- a { display : inline-block; margin-top : .100em; margin-left : 1em; color : #405060; padding : .200em 0 .200em 0; text-decoration : none; border-bottom : 1px dotted; } div { margin-top : .500em; border : 1px solid #ccc; padding : .300em; background-color : #eee; } div div { margin-top : .150em; border : 1px solid #405060; background-color : #ccc; padding : .150em; } --> </style> <script type="text/javascript"> <!-- var hideAndDisplay = function( parentID ) { this.ids = (( document.getElementById ) ? document.getElementById( parentID ) : document.all[ parentID ] ); this.tName = (( this.ids.nodeName ) ? this.ids.nodeName : this.ids.tagName ).toLowerCase(); this.elem = (( document.getElementsByTagName ) ? document.getElementsByTagName( this.tName )[ parentID ] : document.all.tags( this.tName ).item( parentID )); }; hideAndDisplay.prototype = { display : function( thisTagNames, displayProp, thisID ) { this.divs = (( document.getElementsByTagName ) ? this.elem.getElementsByTagName( thisTagNames ) : this.elem.all.tags( thisTagNames )); if ( typeof( thisID ) !== "undefined" ) { try { this.divs[ thisID ].style.display = displayProp; } catch( e ) { this.divs[ thisID ].setAttribute( "style", "display : " + displayProp ); } return; } else { try { this.elem.style.display = displayProp; } catch( e0 ) { this.elem.setAttribute( "style", "display : " + displayProp ); } return; } } }; var $ = function( myDivID, myNodeList, showHideProp, index ) { var eLem = new hideAndDisplay( myDivID ); eLem.display( myNodeList, showHideProp, index ); return; }; window.onload = function() { /* $ Function syntax : $( [ String [, String [, String [, Integer/String ]]]] ); The $() Function comes with four parameters: 1st parameter - ID of the parent ELEMENT that serves as a place holder for the elements' ( or nodeList ) collection. ( REQUIRED ). 2nd parameter - Element reference, ( or Tag Name ) of the element that will be showed/hide from the page ( REQUIRED ). 3rd parameter - display properties ( REQUIRED ). 4th parameter - Specify the Index reference ( or element ID ) that will be showed/hide from the page ( OPTIONAL ). Example of usage : */ $( "div1", "div", "none" ); // By not specifying its 4th parameter, div1 ( 1st parameter, PARENT ELEMENT ) will be set to display as ( 3rd parameter - none ). var elemArray = [ 0, 2, 4 ]; for ( var i = 0; i < elemArray.length; i++ ) { $( "div2", "div", "none", elemArray[ i ] ); // Creating arrays of index for the elements inside div2. } }; // --> </script> </head> <body> <div id="div1"> <!-- This entire collection of elements will not be showed --> <div>Div1 #1</div> <div>Div1 #2</div> <div>Div1 #3</div> <div>Div1 #4</div> <div>Div1 #5</div> </div> <div id="div2"> <!-- element1, element3 and element5 will not be showed --> <div>Div2 #1</div> <div>Div2 #2</div> <div>Div2 #3</div> <div>Div2 #4</div> <div>Div2 #5</div> </div> <div id="nav"> <h6 style="padding-left : 1em; color : #808080; margin-bottom : 0;">- Control Panel -</h6> <!-- Controlling display properties of the element using $() function --> <a href="javascript:void(0)" onclick="$( 'div1', 'div', 'block' )">Show Div1 and all its element</a><br> <a href="javascript:void(0)" onclick="$( 'div2', 'div', 'block', 0 )">Show Div2 and element#1</a><br> <a href="javascript:void(0)" onclick="$( 'div2', 'div', 'block', 2 )">Show Div2 and element#3</a><br> <a href="javascript:void(0)" onclick="$( 'div2', 'div', 'block', 4 )">Show Div2 and element#5</a><br> <a href="javascript:void(0)" onclick="$( 'nav', 'a', 'none' )">Hide Control Panel [ x ]</a> </div> </body> </html>
hope it helps...
![]() |
Similar Threads
- Putting two p or div tags horizontally aligned on same plane (HTML and CSS)
- Unable to apply style to div tags inside form (HTML and CSS)
- XHTML empty DIV tags (HTML and CSS)
- Why do people wish for tableless with CSS? (HTML and CSS)
- About DIV tags (HTML and CSS)
- How do I open a web page with several layers to a spacific layer? (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: AJAX IE7 Working but Mozilla Problem
- Next Thread: how to show page loading Icon..!!
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug captchaformproblem checkbox close codes createrange() css cursor debugger dependent disablefirebug dom download dropdown editor element engine enter error events explorer ext file firefox form forms frameworks getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math media menu microsoft mp4 object onmouseoutdivproblem onreadystatechange paypal pdf php player position programming progressbar prototype redirect regex runtime safari scale scriptlets search security select size software sql text textarea unicode w3c window windowofwords windowsxp wysiwyg \n





