Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Radio button not working properly in Internet explorer to show content on checked

Posted on 2009-07-06
7
Medium Priority
?
981 Views
Last Modified: 2013-12-13
This question  is an extension on a previous question I posted. I have a form with 2 radio buttons that when one is clicked a dropdown is visible and when the other is checked is it not visible.

In firefox this works perfect. In internet explorer it only works when the text next to the radio button is clicked and not when the actual button is checked.

Please tell me what I did wrong.
<script type="text/javascript">
                var prepMenu = function() {
                        var optionNumberThatShowsMenu = 0; //or whichever radio it is, in this case #0
                        
                        var a = document.forms['cart'].options[optionNumberThatShowsMenu];
                        var m = document.forms['cart'].more
                        m.style.display = ( (a.checked === false) ? 'inline' : 'none' );
                }
                
               var loadRadios = function() {
                        for ( var i = 0; i < document.forms['cart'].options.length; i++ ) {
                                var a = document.forms['cart'].options[i];
                                //set up events
                                a.onchange = prepMenu;
                        }
                        //make menu initially visible (or invisible if that's what you want)
                        prepMenu();
                }
        </script>   
 <form action="<?php echo ($_SERVER["PHP_SELF"].(isset($_SERVER["QUERY_STRING"])?"?".htmlentities($_SERVER["QUERY_STRING"]):""));?>" method="post" id="cart" name="cart">
 
 <table width="500" border="0">
                <tr>
                  <td width="236" height="56" align="left" valign="top">
                 Ship Product<input name="options" type="radio" value="B" checked/></td>
				<td width="221" align="left" valign="top"> Pick up from store<input name="options" type="radio" value="0"  />
 
                    </td>
                  <td width="152" align="left" valign="top">
                      <label>
                        <select name="more" id="more">
                          <option>Choose a location</option>
                          <option value="Agoura Hills">Agoura Hills</option>
                          <option value="Canyon Country">Canyon Country</option>
                          <option value="Chatsworth">Chatsworth</option>
                          <option value="Glendale">Glendale</option>
                          <option value="Moorpark">Moorpark</option>
                          <option value="Oxnard">Oxnard</option>
                          <option value="Santa Barbara">Santa Barbara</option>
                          </select>
                        </label>
</td>
                  </tr>
  </table>
</form>

Open in new window

0
Comment
Question by:danjen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 1

Expert Comment

by:Mokona
ID: 24790201
Browsers execute code sequentially, so in your case when you search for the form before the form tag it's impossible for the javascript to find it. Just put your javascript after the form tag.

 <form action="" method="post" id="cart" name="cart">
 
 <table width="500" border="0">
                <tr>
                  <td width="236" height="56" align="left" valign="top">
                 Ship Product<input name="options" type="radio" value="B" checked/></td>
                                <td width="221" align="left" valign="top"> Pick up from store<input name="options" type="radio" value="0"  />
 
                    </td>
                  <td width="152" align="left" valign="top">
                      <label>
                        <select name="more" id="more">
                          <option>Choose a location</option>
                          <option value="Agoura Hills">Agoura Hills</option>
                          <option value="Canyon Country">Canyon Country</option>
                          <option value="Chatsworth">Chatsworth</option>
                          <option value="Glendale">Glendale</option>
                          <option value="Moorpark">Moorpark</option>
                          <option value="Oxnard">Oxnard</option>
                          <option value="Santa Barbara">Santa Barbara</option>
                          </select>
                        </label>
</td>
                  </tr>
  </table>
</form>
 
<script type="text/javascript">
                var prepMenu = function() {
                        var optionNumberThatShowsMenu = 0; //or whichever radio it is, in this case #0
                        
                        var a = document.forms['cart'].options[optionNumberThatShowsMenu];
                        var m = document.forms['cart'].more
                        m.style.display = ( (a.checked === false) ? 'inline' : 'none' );
                }
                
               var loadRadios = function() {
                        for ( var i = 0; i < document.forms['cart'].options.length; i++ ) {
                                var a = document.forms['cart'].options[i];
                                //set up events
                                a.onchange = prepMenu;
                        }
                        //make menu initially visible (or invisible if that's what you want)
                        prepMenu();
                }
                loadRadios();
        </script>   

Open in new window

0
 
LVL 6

Accepted Solution

by:
coolersport earned 2000 total points
ID: 24790213
A much simpler solution (Tested in IE7, IE6, Firefox):

<form action="" method="post" id="cart" name="cart">
 
 <table width="500" border="0">
                <tr>
                  <td width="236" height="56" align="left" valign="top">
                 <label for="shipping">Ship Product</label><input name="options" type="radio" value="B" checked id="shipping" onclick="toggleDropdown();"/></td>
                                <td width="221" align="left" valign="top"> <label for="pickup">Pick up from store</label><input name="options" type="radio" value="0" id="pickup" onclick="toggleDropdown();" />
 
                    </td>
                  <td width="152" align="left" valign="top">
                      <label>
                        <select name="more" id="more" style="display:none">
                          <option>Choose a location</option>
                          <option value="Agoura Hills">Agoura Hills</option>
                          <option value="Canyon Country">Canyon Country</option>
                          <option value="Chatsworth">Chatsworth</option>
                          <option value="Glendale">Glendale</option>
                          <option value="Moorpark">Moorpark</option>
                          <option value="Oxnard">Oxnard</option>
                          <option value="Santa Barbara">Santa Barbara</option>
                          </select>
                        </label>
</td>
                  </tr>
  </table>
   <script type="text/javascript">
               var toggleDropdown = function() {
					document.getElementById('more').style.display = document.getElementById('pickup').checked ? 'block' : 'none';
                };
        </script>   
</form>

Open in new window

0
 
LVL 6

Expert Comment

by:coolersport
ID: 24790229
Mokona, you missed his question. The problem is with clicking on the radio button itself doesn't trigger the event. Using element ids and label tag is a better way to go, I reckon :)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

by:coolersport
ID: 24790235
The fact (for this case) that the script can be loaded before or after, it doesn't matter as it doesn't get called while the page is loading. The function is only called when user click on the radios.
0
 
LVL 1

Expert Comment

by:Mokona
ID: 24790250
I simply corrected his code and has i posted it works fine, but is true that your's is cleaner.
0
 

Author Closing Comment

by:danjen
ID: 31600404
thank you so much. That is much better!
0
 
LVL 6

Expert Comment

by:coolersport
ID: 24790270
Hi Mokona,

You missed it again. He meant it doesn't work in IE. I tested yours in IE before letting you know though.

Cheers,
Tien
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question