Solved

jQuery Toggle Text Box When Clicking Radio

Posted on 2014-11-11
2
1,298 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
Comment Utility
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
Comment Utility
Thank you!
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

Suggested Solutions

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

728 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