Solved

disable a <div functions by radio button

Posted on 2007-03-23
6
1,818 Views
Last Modified: 2012-05-05
I have two radio buttons and two section declared by <div> tab. Now I want to

1. by default, <div> 1 is open and <div> 2 is disable, not hidden
2. By clicking  radio button 2, <div> 2 open and <div> 1 become disabled
3. By clicking radio button 1, <div> 1 open and <div> 2 become disabled

How can I do that? It is possible not to use JS but HTML? JS is fine to me too if i have to.

Here is my code

<div>
    <form name="IP_to_MAC" action="<portlet:actionURL/>" method="post" onSubmit="return ValidateForm()" >
       
            <div style="padding-top:5px;padding-bottom:5px;" id="ICC">
              <fieldset class="portlet-font">
                <legend >Type</legend>
                <input name="VolContID" type="hidden" value="999" >
               
         
                <input name="Sch_IP_Addr" type="radio" value="Sch_IP_Addr" >Search for IP Address                              
                <input name="Sch_MAC_Addr" type="radio" value="Sch_MAC_Addr"  checked  >Search for MAC Address
                <br>
                </fieldset>
<br>
                   <div >
                        <label>Search for MAC Address: <input type="text" name="MACAddress" size="20" maxlength="25" > </label>


                       <label>on the:
                           <select  id="netname" name="netname" type="text" >

                                 <option selected  >uiuc-citesstaff-net</option>
                                   <option >uiuc-bioeng-net</option>
                                     <option >uiuc-teste-net</option>


                           </select>
                      </label>
 <br>
                   </div>
       
                    <div class="disabled" style="padding-top:5px;padding-bottom:5px; visibility;" id="IPC" >

                        <label>Search for IP Address: <input type="text" name="IPAddress" size="20" maxlength="25" > </label>


                        <label>on the MAC Address:
                            <select id="MACname" name="MACname" type="text" >

                                  <option selected  >00:0A:95:77:C3:CE</option>
                                    <option>00:06:5B:62:D5:EB</option>
                                      <option  >00:06:5B:A6:AD:EC</option>

                            </select>
                        </label>

                    </div>
       
 
0
Comment
Question by:wasabi3689
  • 2
6 Comments
 
LVL 35

Expert Comment

by:mrichmon
Comment Utility
>>It is possible not to use JS but HTML
No.

You must use javascript if you want it to change based on user interaction (such as clicking a radion button)

>>1. by default, <div> 1 is open and <div> 2 is disable, not hidden
You can't disable a div, but you could disable each input inside, then use javascript to toggle the inputs/selects/etc within the two divs between disabled and enabled.

You would use

to disable:
document.getElementById("IdNameHere").style.disabled = true;

to enable:
document.getElementById("IdNameHere").style.disabled = false;

Repeat for each input/select/etc

To trigger use the onclick of the radio button to detect which one is selected and set the statuses accordingly.
0
 

Author Comment

by:wasabi3689
Comment Utility
Here is what I code, but it doesn't work. Can you figure out the problem?

<script language="JavaScript">
               
                    function toggleVisibility(me){
                    if (document.getElementById("MAC").style.disabled = true){
                      document.getElementById("IP").style.disabled = false;
                            }
                        else {
                        document.getElementById("IP").style.disabled = true;
                    }
                }
                </script>
               
         
                <input name="Sch_IP_Addr" type="radio" value="Sch_IP_Addr" onclick="toggleVisibility(me)" >Search for IP Address                              
                <input name="Sch_MAC_Addr" type="radio" value="Sch_MAC_Addr"  checked onclick="toggleVisibility(me)"  >Search for MAC Address
                <br>
                </fieldset>
<br>
                   <div id="MAC" style="disabled" >
                        <label>Search for MAC Address: <input type="text" name="MACAddress" size="20" maxlength="25" > </label>


                       <label>on the:
                           <select  id="netname" name="netname" type="text" >

                                 <option selected  >uiuc-citesstaff-net</option>
                                   <option >uiuc-bioeng-net</option>
                                     <option >uiuc-teste-net</option>


                           </select>
                      </label>
 
                   </div>
   <br>    
                    <div id="IP" style="padding-top:5px;padding-bottom:5px; visibility;" id="IPC" >

                        <label>Search for IP Address: <input type="text" name="IPAddress" size="20" maxlength="25" > </label>


                        <label>on the MAC Address:
                            <select id="MACname" name="MACname" type="text" >

                                  <option selected  >00:0A:95:77:C3:CE</option>
                                    <option>00:06:5B:62:D5:EB</option>
                                      <option  >00:06:5B:A6:AD:EC</option>

                            </select>
                        </label>

                    </div>
       
         
          </div>
0
 

Author Comment

by:wasabi3689
Comment Utility
Why it doesn't work

I modify the code as bellow

     <script language="JavaScript">
               
                    function toggleVisibility(me){
                   {
                     
                     
                     if (document.IP_to_MAC.MACname.disabled = true) {
                       
       
                        document.IP_to_MAC.MACAddress.disabled = false;
                        document.IP_to_MAC.netname.disabled = false;
                      }
                           
                        }
                       
                        function toggleVisibility2(me){
                       {
                     
                             document.IP_to_MAC.MACname.disabled = false;
                             document.IP_to_MAC.IPAddress.disabled = false;
                     
                              document.IP_to_MAC.MACAddress.disabled = true;
                              document.IP_to_MAC.netname.disabled = true;
                     
                       
                        }
                       
               
                </script>
               
         
                <input name="Sch_IP_Addr" type="radio" value="Sch_IP_Addr" onclick="toggleVisibility(me)" >Search for IP Address                              
                <input name="Sch_MAC_Addr" type="radio" value="Sch_MAC_Addr"  checked onclick="toggleVisibility2(me)"  >Search for MAC Address
                <br>
                </fieldset>
<br>
                   <div id="MAC" >
                        <label>Search for MAC Address: <input type="text" name="MACAddress" size="20" maxlength="25" > </label>


                       <label>on the
                           <select  id="netname" name="netname" type="text" >

                                 <option selected  >uiuc-citesstaff-net</option>
                                   <option >uiuc-bioeng-net</option>
                                     <option >uiuc-teste-net</option>


                           </select>
                      </label>
 
                   </div>
   
                    <div id="IPC"  style="padding-top:5px;padding-bottom:5px; visibility:disabled;" >

                        <label>Search for IP Address: <input type="text" name="IPAddress" size="20" maxlength="25" disabled  > </label>

                        <label>on the MAC Address:
                            <select id="MACname" name="MACname" type="text" disabled >

                                  <option selected >00:0A:95:77:C3:CE</option>
                                    <option>00:06:5B:62:D5:EB</option>
                                      <option  >00:06:5B:A6:AD:EC</option>

                            </select>
                        </label>

                    </div>
       
         
          </div>
         

I have two onclick for two radio buttons.
When I click radio button "Search for IP Address",  the input fields "Search For MAC Address" is not auto disbled? Why ?
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 50 total points
Comment Utility
Too many syntax errors.

Here is a version I think works how you want it.
Change the IP and !IP around if I was wrong

<html>
<head>

<script language="JavaScript">
function toggleVisibility(theRad) {
  var IP = (theRad.value=='Sch_IP_Addr')?true:false;
  theRad.form.IPAddress.disabled = !IP;
  theRad.form.MACname.disabled = !IP;
  theRad.form.MACAddress.disabled = IP;
  theRad.form.netname.disabled = IP;
}
</script>
</head>
<body onLoad="if(document.forms[0].Sch_Addr[0].checked) toggleVisibility(document.forms[0].Sch_Addr[0]); else toggleVisibility(document.forms[0].Sch_Addr[1])">
<form>
<input name="Sch_Addr" type="radio" value="Sch_IP_Addr" onclick="toggleVisibility(this)" >Search for IP Address
<input name="Sch_Addr" type="radio" value="Sch_MAC_Addr" checked onclick="toggleVisibility(this)" >Search for MAC Address
<br>
</fieldset>
<br>
<div id="MAC" >
<label>Search for MAC Address: <input type="text" name="MACAddress" size="20" maxlength="25" > </label>


<label>on the
<select id="netname" name="netname" type="text" >

<option selected >uiuc-citesstaff-net</option>
<option >uiuc-bioeng-net</option>
<option >uiuc-teste-net</option>


</select>
</label>

</div>

<div id="IPC" style="padding-top:5px;padding-bottom:5px; visibility:disabled;" >

<label>Search for IP Address: <input type="text" name="IPAddress" size="20" maxlength="25"> </label>

<label>on the MAC Address:
<select id="MACname" name="MACname" type="text">

<option selected >00:0A:95:77:C3:CE</option>
<option>00:06:5B:62:D5:EB</option>
<option >00:06:5B:A6:AD:EC</option>

</select>
</label>

</div>


</div>
</form>
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now