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
Solved

jQuery Toggle Text Box When Clicking Radio

Posted on 2014-11-11
2
1,366 Views
Last Modified: 2014-11-11
I have a form with two radio buttons and a text box.  I need the textbox to be hidden on page load, and when the radio is checked to "yes" it shows the text box, when the radio is checked back to no, it re-hides the text box.

Here is the form:

            <form class="form-horizontal" role="form">
              <div class="form-group">
                <div class="col-sm-3">Enter your email address</div>
                <div class="col-sm-9"><input type="email" class="form-control" id="InputEmail" placeholder="Email"></div>
              </div>

              <h3>DO YOU HAVE A PASSWORD?</h3>

              <div class="form-group">
                <div class="col-sm-12"><input type="radio" name="PasswordToggle" id="PasswordToggle" value="No" checked="checked">  No (you can create an account later)</div>
              </div>

              <div class="form-group">
                <div class="col-sm-3"><input type="radio" name="PasswordToggle" id="PasswordToggle" value="Yes">  Yes, my password is </div>
                <div class="col-sm-9"><input type="password" class="form-control" id="InputPassword" placeholder="Password"></div>
              </div>

              <div class="form-group">
                <div class="col-sm-12"><button type="button" class="btn btn-success" onclick="window.location='/login'">Continue <i class="glyphicon glyphicon-circle-arrow-right"></i></button></div>
              </div>
            </form>

Open in new window


The radios are PasswordToggle and the text box to hide is InputPassword

Thanks
0
Comment
Question by:net-workx
2 Comments
 
LVL 8

Accepted Solution

by:
vr6r earned 500 total points
ID: 40436284
Try this... updated your html with the following changes.
1. Removed the ID from the radio buttons in place of class = "pwdToggle")
2. Made InputPassword hidden by default.

<form class="form-horizontal" role="form">
              <div class="form-group">
                <div class="col-sm-3">Enter your email address</div>
                <div class="col-sm-9"><input type="email" class="form-control" id="InputEmail" placeholder="Email"></div>
              </div>

              <h3>DO YOU HAVE A PASSWORD?</h3>

              <div class="form-group">
                <div class="col-sm-12"><input type="radio" name="PasswordToggle" class="pwdToggle" value="No" checked="checked">  No (you can create an account later)</div>
              </div>

              <div class="form-group">
                <div class="col-sm-3"><input type="radio" class="pwdToggle" name="PasswordToggle" value="Yes">  Yes, my password is </div>
                <div class="col-sm-9"><input type="password" class="form-control" id="InputPassword" placeholder="Password" style="display:none;"></div>
              </div>

              <div class="form-group">
                <div class="col-sm-12"><button type="button" class="btn btn-success" onclick="window.location='/login'">Continue <i class="glyphicon glyphicon-circle-arrow-right"></i></button></div>
              </div>
            </form>

Open in new window


js... on click, test value of radio button, if "Yes" then show textbox, if "No" then hide it.
$(function(){
    $('.pwdToggle').click(function(e){
       if($(e.target).val() == "Yes")
           $('#InputPassword').show();
        else
            $('#InputPassword').hide();
    });
});

Open in new window


Working version as a fiddle: http://jsfiddle.net/mtr801f6/

Hope this helps.
0
 

Author Comment

by:net-workx
ID: 40436291
Thank you!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 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