user3361043
user3361043

Reputation: 371

Hide/show textboxes with jQuery based on radio button selection

I need for certain textboxes to show up when a certain radio button is clicked on my form, otherwise the textboxes should remain hidden. Here's an example of what I have:

HTML:

Radio buttons:
<p>Show textboxes<input type="radio" name="radio1" value="Show" onClick="getResults()">Do nothing<input type="radio" name="radio1" value="Nothing"></p>
Textboxes:
<div class="text"><p>Textbox #1 <input type="text" name="text1" id="text1" maxlength="30"></p></div>
<div class="text"><p>Textbox #2 <input type="text" name="text2" id="text2" maxlength="30"></p></div>

jQuery:

$(document).ready(function() {
  $(".text").hide()
    function getResults() {
        $(".text").show()
    };
});

Fiddle

When I take out the .show() code, the textboxes stay hidden. I need to show them when a radio button with the onClick event gets selected. Any help would be much appreciated.

Upvotes: 4

Views: 45084

Answers (9)

Don Zanurano
Don Zanurano

Reputation: 15

 1. $("input[name='Radio1']").change(function(){
           if($(this).val()=="Ya") {
                $("#QUESTION_1").fadeIn(250);
           }
           else {
               $("#QUESTION_1").fadeOut(250); 
           }        });

Upvotes: 1

Mairaj Rasool
Mairaj Rasool

Reputation: 1

                        <div class="RdoClick">
                            <asp:RadioButton ID="RadioSugges" runat="server" GroupName="PopupRadio" Text="&nbsp;&nbsp;&nbsp;C. Insert your suggestion:" />
                        </div>
                        <asp:TextBox ID="TextBox1" runat="server" class="PopupTxtBox" PlaceHolder="Enter your suggestion..."
                            onClick="check();" TabIndex="1" />

<script type="text/javascript">
    function check() {
        document.getElementById('<%=RadioSugges.ClientID%>').checked = true;
    }

</script>

Upvotes: 0

pj013
pj013

Reputation: 1419

Check this demo jsFiddle

jQuery

$(document).ready(function() {
    $(".text").hide()
    $('[name=radio1]').on('change', function(){
        $('.text').toggle(this.value === 'Show');
    })
});

Upvotes: 2

A. Wolff
A. Wolff

Reputation: 74420

Redefine a little bit your HTML markup and then you could only use CSS:

DEMO jsFiddle

input[value=Nothing]:checked ~ .text{
    display: none;
}

Upvotes: 2

Rich
Rich

Reputation: 4170

JSFIDDLE

Javascript

$(document).ready(function () {
    $(".text").hide();
    $("#r1").click(function () {
        $(".text").show();
    });
    $("#r2").click(function () {
        $(".text").hide();
    });
});

Html

<p>Show textboxes
    <input type="radio" name="radio1" id="r1" value="Show">Do nothing
    <input type="radio" name="radio1" id="r2" value="Nothing">
</p>Wonderful textboxes:
<div class="text">
    <p>Textbox #1
        <input type="text" name="text1" id="text1" maxlength="30">
    </p>
</div>
<div class="text">
    <p>Textbox #2
        <input type="text" name="text2" id="text2" maxlength="30">
    </p>
</div>

Upvotes: 10

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

Currently you can pass the clicked object as this in form of parameter to getResults(), Where this refers to the current clicked object

See below

Script

$(document).ready(function() {
    $(".text").hide()

});
function getResults(elem) {
    elem.checked && elem.value == "Show" ? $(".text").show() : $(".text").hide();
};

HTML

<input type="radio" name="radio1" value="Show" onClick="getResults(this)">
Do nothing                                                         ^^^^
<input type="radio" name="radio1" value="Nothing" onclick="getResults(this)">
                                                                      ^^^^ 

Fiddle --> http://jsfiddle.net/tT48f/7/

Upvotes: 2

Girish Vadhel
Girish Vadhel

Reputation: 745

Try like:

$(document).ready(function() {
  $(".text").hide();

});

function getResults() {
        $(".text").show();
    };

Upvotes: 1

Karl-Andr&#233; Gagnon
Karl-Andr&#233; Gagnon

Reputation: 33870

You can use this code :

$(document).ready(function() {
    $(".text").hide()
    $('[name=radio1]').on('change', function(){
        $('.text').toggle(this.value === 'Show');
    })
});

Inline event are not recomanded, so bind a change event the the radio and if the value is Show, well show it!

http://jsfiddle.net/tT48f/3/

Upvotes: 2

Felix
Felix

Reputation: 38102

Instead of inline onclick() javascript, you can use .click():

$(document).ready(function () {
    $(".text").hide();
    $('input[type="radio"]').eq(0).click(function() {
        $(".text").show()
    });
});

Updated Fiddle

However, you should use .change() event for the input elements:

$(document).ready(function () {
    $(".text").hide();
    $('input[type="radio"]').eq(0).change(function() {
        $(".text").show()
    });
});

Updated Fiddle

Upvotes: 2

Related Questions