Reputation: 371
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()
};
});
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
Reputation: 15
1. $("input[name='Radio1']").change(function(){
if($(this).val()=="Ya") {
$("#QUESTION_1").fadeIn(250);
}
else {
$("#QUESTION_1").fadeOut(250);
} });
Upvotes: 1
Reputation: 1
<div class="RdoClick">
<asp:RadioButton ID="RadioSugges" runat="server" GroupName="PopupRadio" Text=" 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
Reputation: 1419
Check this demo jsFiddle
$(document).ready(function() {
$(".text").hide()
$('[name=radio1]').on('change', function(){
$('.text').toggle(this.value === 'Show');
})
});
Upvotes: 2
Reputation: 74420
Redefine a little bit your HTML markup and then you could only use CSS:
input[value=Nothing]:checked ~ .text{
display: none;
}
Upvotes: 2
Reputation: 4170
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
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
Reputation: 745
Try like:
$(document).ready(function() {
$(".text").hide();
});
function getResults() {
$(".text").show();
};
Upvotes: 1
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!
Upvotes: 2
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()
});
});
However, you should use .change()
event for the input
elements:
$(document).ready(function () {
$(".text").hide();
$('input[type="radio"]').eq(0).change(function() {
$(".text").show()
});
});
Upvotes: 2