Solved

Help javascript radio button

Posted on 2014-04-07
4
473 Views
Last Modified: 2014-04-08
Javascript - can a radio button options dependent on other radio button choices? My javascript below show no changes  


<script type="text/javascript">

      function setshowName(passvar)
      {
      
             
            if ( passvar == 'T')
            {
                  document.getElementById('showName').value = "yes";
                        }
            else
            {

                  document.getElementById('showName').value = "no";
                  
            }
      
      }
</script>





<form name="testform" method="post">
<table>
  <tr>
      <td>
      Show Address:
        <INPUT TYPE="RADIO" NAME="showaddr" VALUE="A" onclick="setshowName('A')" checked>Show A Address
      <INPUT TYPE="RADIO" NAME="showaddr" VALUE="B" onclick="setshowName('B')">Show B Address
      </td>
  </tr>
  <tr>
      <td>
        Show Test Name:
        <INPUT TYPE="RADIO" NAME="showName" VALUE="yes" checked>yes
        <INPUT TYPE="RADIO" NAME="showName" VALUE="no">no
     </td>
   </tr>
</table>
</form>
0
Comment
Question by:jfreeman2010
  • 2
  • 2
4 Comments
 

Author Comment

by:jfreeman2010
Comment Utility
I want to make the JavaScript will do:

When click on Show A Address, show test name will checked 'yes',
when click on Show B Address, show test name will checked 'no'

Thank you
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
It doesn't work that way.  In addition, you didn't have 'id's for the 'showname' radio buttons.  Try this instead.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Untitled</title>
<script type="text/javascript">

      function setshowName(passvar)
      {
      
             
            if ( passvar == 'A')
            {
                  document.getElementById('showName1').checked = true;
                  document.getElementById('showName2').checked = false;
                        }
            if ( passvar == 'B')
            {

                  document.getElementById('showName1').checked = false;
                  document.getElementById('showName2').checked = true;
                  
            }
      
      }
</script>
</head>
<body>


<form name="testform" method="post">
<table>
  <tr>
      <td>
      Show Address:
      <INPUT TYPE="RADIO" NAME="showaddr" VALUE="A" onclick="setshowName('A')" checked>Show A Address
      <INPUT TYPE="RADIO" NAME="showaddr" VALUE="B" onclick="setshowName('B')">Show B Address
      </td>
  </tr>
  <tr>
      <td>
        Show Test Name:
        <INPUT TYPE="RADIO" NAME="showName" id="showName1" VALUE="yes" checked>yes
        <INPUT TYPE="RADIO" NAME="showName" id="showName2" VALUE="no">no
     </td>
   </tr>
</table>
</form> 
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:jfreeman2010
Comment Utility
Dave,

Thank you for your help!!  It works.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You're welcome, glad to help.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

12 Experts available now in Live!

Get 1:1 Help Now