Reputation: 411
I want to show the ptext when radio button has checked and hide when another radio button is active but does not work
This is my code:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
</head>
<body>
<input id="postageyes" name="type" value="Paint" type="radio">Paint
<br>
<input id="postageno" name="type" value="Sculpture" type="radio">Sculpture
<br>
<input id="postageno" name="type" value="calligraphy" type="radio">calligraphy
<br>
<input id="stext" name="type" value="type" type="text"><br>
<input id="ctext" name="type" value="type" type="text"><br>
<input id="ptext" name="type" value="type" type="text">
<script type="text/javascript">
$('input:radio[name="postage"]').change(function() {
if ($(this).val() == 'Paint') {
$("#ptext").show();
} else {
$("#ptext").hide();
}
});
</script>
</body>
</html>
Upvotes: 0
Views: 392
Reputation: 276
HTML - The fix was only in the name attribute, becase in your html says "Type" instead of "postage"
<input id="postageyes" name="postage" value="Paint" type="radio">Paint
<br>
<input id="postageno" name="postage" value="Sculpture" type="radio">Sculpture
<br>
<input id="postageno" name="postage" value="calligraphy" type="radio">calligraphy
<br>
<input id="stext" name="type" value="type" type="text"><br>
<input id="ctext" name="type" value="type" type="text"><br>
<input id="ptext" name="type" value="type" type="text" style="display:none">
Javascript - The javascript if working I only will give you a recommendation and is, use the document ready
$(document).ready(function() {
$('input:radio[name="postage"]').change(function() {
if ($(this).val() == 'Paint') {
$("#ptext").show();
} else {
$("#ptext").hide();
}
});
});
Upvotes: 0
Reputation: 167162
You have two major issues:
id
should be unique.input
with name
as postage
.ptext
when loading the page.Change the name
to postage
:
<input id="postageyes" name="postage" value="Paint" type="radio">Paint
<br>
<input id="postageno1" name="postage" value="Sculpture" type="radio">Sculpture
<br>
<input id="postageno2" name="postage" value="calligraphy" type="radio">calligraphy
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="postageyes" name="postage" value="Paint" type="radio">Paint
<br>
<input id="postageno1" name="postage" value="Sculpture" type="radio">Sculpture
<br>
<input id="postageno2" name="postage" value="calligraphy" type="radio">calligraphy
<br>
<input id="stext" name="type" value="type" type="text"><br>
<input id="ctext" name="type" value="type" type="text"><br>
<input id="ptext" name="type" value="type" type="text">
<script type="text/javascript">
$("#ptext").hide();
$('input:radio[name="postage"]').change(function() {
if ($(this).val() == 'Paint') {
$("#ptext").show();
} else {
$("#ptext").hide();
}
});
</script>
Also, it is better to hide it when you load the document. See the above example.
Upvotes: 1
Reputation: 4870
You have to pass wrong radio button name in jQuery
, Could you please check below code?
<script type="text/javascript">
$('input:radio[name="type"]').change(function() {
if ($(this).val() == 'Paint') {
$("#ptext").show();
} else {
$("#ptext").hide();
}
});
</script>
Upvotes: 0