Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Unhappy 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
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    waterintofunk (08-19-2011)

  • #3
    New Coder
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Smile

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

  • #4
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Have you tried using css

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


    you can say

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

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •