Web Analytics Made Easy -
StatCounter Show/Hide div issues with Firefox - CodingForum

Announcement

Collapse
No announcement yet.

Show/Hide div issues with Firefox

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Show/Hide div issues with Firefox

    Hello,

    For starters I'm a real javascript noob. The problem is I've got some code which which should display the contents of a div depending on which of two radio buttons are selected. Works fine in IE, but in Firefox, or even Safari it will display both divs if both are clicked. In other words it won't hide the div while it displays the other.

    The code is below, and any help would be appreciated.


    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
     <TITLE> Help! </TITLE>
    
    <style type="text/css">
    .hide {display: none}
    </style>
    
    <script type="text/javascript">
    function hideDivArea(str_area_hide){
    tag = document.getElementsByTagName("div");
    for(x=0;x<tag.length; x++)
    {
    if(tag[x].getAttribute('id').indexOf(str_area_hide) != -1)
    {
    tag[x].style.display = "none";
    }
    }
    }
    function showDivArea(areas_show, areas_hide){
    
    for (var i = 0; i < areas_show.length; i++)
    {
    ge = document.getElementById(areas_show[i]);
    ge.style.display = "block";
    }
    for (var i = 0; i < areas_hide.length; i++)
    {
    hideDivArea(areas_hide[i]);
    }
    }
    </script>
    </HEAD>
    
    <BODY>
    
    				<form action="feedout.php" method="post">
    				<input type="hidden" name="action" value="sendmail" />
    	                      <table style="width: 435px" border="0" cellspacing="0" cellpadding="0">
    	                        <tbody>
    	                          <tr>
    	                            <td height="15" valign="bottom">Do you Own or Rent?:<span class="redtext">*</span></td>
    	                            <td valign="bottom">&nbsp;</td>	
    	                          </tr>
    	                          <tr height="30">
    	                            <td colspan="2">
    								<div>
    									<input type="radio" name="ownRent" value="own" size="34" onclick="javascript:showDivArea(['own'], ['rent'])">Own
    									<input type="radio" name="ownRent" value="rent" size="34" onclick="javascript:showDivArea(['rent'], ['own'])">Rent
    
    									<div class="hide" id="own">
    										Lawyer Name:<br />
    										<input type="text" name="lawyerName" size="34"><br />
    										Address:<br />
    										<input type="text" name="lawyerAddress" size="34"><br />
    										Telephone Number:<br />
    										<input type="text" name="lawyerPhone" size="34"><br />
    									</div>
    									<div class="hide" id="rent">
    										Landlord Name:<br />
    										<input type="text" name="landlordName" size="34"><br />
    										Address:<br />
    										<input type="text" name="landlordAddress" size="34"><br />
    										Telephone Number:<br />
    										<input type="text" name="landlordPhone" size="34"><br />
    									</div>
    								</div>
    								</td>
    	                          <tr>
    	                            <td>
    	                            <table width="240" border="0" cellspacing="0" cellpadding="0">
    	                              <tbody>
    	                                <tr>
    	                                  <td width="105" colspan="1" valign="middle" bordercolor="#000000" bgcolor="#ffffff">
    										<input style="border: solid 1px black;" type="submit" value="Submit Form">
    	                                  </td>
    	                                  <td width="20" height="35"> &nbsp;&nbsp;
    	                                  </td>
    	                                  <td width="54" valign="middle"><input  style="border: solid 1px black;" name="reset" type="reset" value="Clear Form"> </span></td>
    	                                </tr>
    	                              </tbody>
    	                            </table>
    	                            </td>
    	                            <td>&nbsp;</td>
    	                          </tr>
    	                        </tbody>
    	                      </table>
    	               </form>
    </BODY>
    </HTML>

  • #2
    I think that code is *WAY* overly complex.

    Change these lines:
    Code:
    <script type="text/javascript">
    function showRentOrOwn(which)
    {
        document.getElementById("own").style.display = which=="own" ? "block" : "none";
        document.getElementById("rent").style.display = which=="rent" ? "block" : "none";
    }
    </script>
    
    ...
    
    	<label><input type="radio" name="ownRent" value="own" size="34" onclick="showRentOrOwn('own')">Own</label>
    	<label><input type="radio" name="ownRent" value="rent" size="34" onclick="showRentOrOwn('rent')">Rent</label>
    
    ...
    
         <td width="54" valign="middle">
              <input  style="border: solid 1px black;" name="reset" type="reset" 
                     value="Clear Form" onclick="showRentOrOwn('neither');"> 
          </td>
    You don't have to add the <label>s, but they allow the user to click on the word or the radio button.

    You need to add that call in the reset button or the last shown div will stay shown.
    Be yourself. No one else is as qualified.

    Comment


    • #3
      Wow! Thank you. Also, extra thanks for pointing out the lack of label tags.

      Comment


      • #4
        Have you tried using css

        p.show { display: block }
        p.hide { display: none; }


        you can say

        document.getElementById("id").className="hide";

        Comment


        • #5
          100% agreed. But the important part is that with only the two <div>s, the easiest solution is to simply set each one's display (directly or by classname) on each radio button change.
          Be yourself. No one else is as qualified.

          Comment

          Working...
          X