hide some divs, show some others (but not all)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dusk
    New Member
    • Jan 2008
    • 7

    hide some divs, show some others (but not all)

    Hi,

    I have a page with lots of hidden divs which are revealed based on choices made at each 'layer'.

    So I've used naming convention which represents the order in which each div becomes visible - 'a100', 'a200', 'a300' for the first 'layer'; 'b100', 'b200', 'b300' for the second; 'c100, 'c200', 'c300' for the third ... etc

    I'm writing the javascript functions to hide and show the appropriate divs and its getting out of hand pretty quickly - I have to 'showdiv' each div in the sequence that the user chooses, and also 'hidediv' every other choice which could potentially have been made (since if a user goes back a step in the sequence and clicks on a different choice it needs to hide what has already been displayed).

    What I'm after is a way to use 'hidediv' to span multiple divs using the naming convention above - like using a partial name plus asterisk to find all the names which match the partial name given. But preferably it would recognise alphanumeric order...

    Like if I could use:

    [html]<A href="javascrip t:hidediv('b*') ">click here</A>[/html]

    to hide all divs with ids beginning with 'b' or later in the alphabet

    Here's the javascript:

    [html]

    <script language="JavaS cript">

    function hidediv(id) {
    //safe function to hide an element with a specified id
    if (document.getEl ementById) { // DOM3 = IE5, NS6
    document.getEle mentById(id).st yle.display = 'none';
    }
    else {
    if (document.layer s) { // Netscape 4
    document.id.dis play = 'none';
    }
    else { // IE 4
    document.all.id .style.display = 'none';
    }
    }
    }

    function showdiv(id) {
    //safe function to show an element with a specified id

    if (document.getEl ementById) { // DOM3 = IE5, NS6
    document.getEle mentById(id).st yle.display = 'block';
    }
    else {
    if (document.layer s) { // Netscape 4
    document.id.dis play = 'block';
    }
    else { // IE 4
    document.all.id .style.display = 'block';
    }
    }
    }

    </script>

    [/html]

    and here's some html:

    [html]

    choose <A href="javascrip t:showdiv('a100 ');hidediv('a20 0');hidediv('b1 00');hidediv('b 200');hidediv(' c100');hidediv( 'c200')">a100</A> or <A href="javascrip t:showdiv('a200 ');hidediv('a10 0');hidediv('b1 00');hidediv('b 200');hidediv(' c100');hidediv( 'c200')">a200</A>
    <DIV id=a100 style="DISPLAY: none">a100 content<BR><BR> choose <A href="javascrip t:showdiv('b100 ');hidediv('b20 0');hidediv('c1 00);hidediv(c20 0')">b100</A> or <A href="javascrip t:showdiv('b200 ');hidediv('b10 0');hidediv('c1 00');hidediv('c 200')">b200</A></DIV>
    <DIV id=a200 style="DISPLAY: none">a200 content<BR><BR> choose <A href="javascrip t:showdiv('b100 ');hidediv('b20 0');hidediv('c1 00);hidediv(c20 0')">b100</A> or <A href="javascrip t:showdiv('b200 ');hidediv('b10 0');hidediv('c1 00');hidediv('c 200')">b200</A></DIV>
    <DIV id=b100 style="DISPLAY: none">b100 content<BR><BR> choose <A href="javascrip t:showdiv('c100 ');hidediv('c20 0')">c100</A> or <A href="javascrip t:showdiv('c200 ');hidediv('c10 0')">c200</A></DIV>
    <DIV id=b200 style="DISPLAY: none">b200 content<BR><BR> choose <A href="javascrip t:showdiv('c100 ');hidediv('c20 0');">c100</A> or <A href="javascrip t:showdiv('c200 ');hidediv('c10 0');">c200</A></DIV>
    <DIV id=c100 style="DISPLAY: none">this is c100 content</DIV>
    <DIV id=c200 style="DISPLAY: none">this is c200 content</DIV>

    [/html]
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    [html]<A href="javascrip t:hidediv('b', 1)">click here</A>[/html]

    [CODE=javascript]function hidediv(id, ALL) {
    //safe function to hide an element with a specified id
    //if ALL flag is on, hide all starting with what provided as id

    if (document.getEl ementById) { // DOM3 = IE5, NS6
    if (ALL) {
    var divs = document.getEle mentsByTagName( 'div');
    for (var i=0; i<divs.length; i++)
    if (divs[i].id.match('^'+i d))
    document.getEle mentById(id).st yle.display = 'none';
    }
    else document.getEle mentById(id).st yle.display = 'none';
    }

    //change the remaining accordingly... (it don't think it will work for IE4)
    }
    else {
    if (document.layer s) { // Netscape 4
    document.id.dis play = 'none';
    }
    else { // IE 4
    document.all.id .style.display = 'none';
    }
    }
    }[/CODE]

    Comment

    • dusk
      New Member
      • Jan 2008
      • 7

      #3
      ok I've tried this but I can't get it to work... it gives me an 'error on page' message when I try using this

      [html]

      <A href="javascrip t:hidediv('a', 1)">click here</A> to hide

      [/html]

      and if I give it the exact name of a div, it hides that div correctly but not the others:

      [html]

      <A href="javascrip t:hidediv('a100 ', 1)">click here</A> to hide

      [/html]

      here's my code:


      Code:
      function showdiv(id) {
      	//safe function to show an element with a specified id
      		  
      	if (document.getElementById) { // DOM3 = IE5, NS6
      		document.getElementById(id).style.display = 'block';
      	}
      	else {
      		if (document.layers) { // Netscape 4
      			document.id.display = 'block';
      		}
      		else { // IE 4
      			document.all.id.style.display = 'block';
      		}
      	}
      }
      
      
      function hidediv(id, ALL) {
          //safe function to hide an element with a specified id
          //if ALL flag is on, hide all starting with what provided as id 
       
          if (document.getElementById) { // DOM3 = IE5, NS6
              if (ALL) {
                  var divs = document.getElementsByTagName('div');
                  for (var i=0; i<divs.length; i++)
                  if (divs[i].id.match('^'+id))
                  document.getElementById(id).style.display = 'none';
              }
              else document.getElementById(id).style.display = 'none';
          }
      }

      [html]

      click here to show:<BR><BR><B R>

      <A href="javascrip t:showdiv('a100 ')">a100</A><BR><BR><BR>

      <A href="javascrip t:showdiv('a100 2')">a1002</A><BR><BR><BR>

      <A href="javascrip t:showdiv('a100 3')">a1003</A><BR><BR><BR>

      <div id=a100 style="DISPLAY: none">div a100 content</DIV>

      <div id=a1002 style="DISPLAY: none">div a1002content</DIV>

      <div id=a1003 style="DISPLAY: none">div a1003content</DIV>


      <BR><BR>

      <A href="javascrip t:hidediv('a', 1)">click here</A> to hide

      [/html]

      Comment

      Working...