show()
and hide()
methodsYou can simply use the jQuery show()
and hide()
methods to show and hide the div elements based on the selection of radio buttons. The div boxes in the following example are hidden by default using the CSS display
property which value is set to none
.
Let's try this example by selecting the different radio buttons and see how it works:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Elements Using Radio Buttons</title>
<style>
.box{
color: #fff;
padding: 20px;
display: none;
margin-top: 20px;
}
.red{ background: #ff0000; }
.green{ background: #228B22; }
.blue{ background: #0000ff; }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var inputValue = $(this).attr("value");
var targetBox = $("." + inputValue);
$(".box").not(targetBox).hide();
$(targetBox).show();
});
});
</script>
</head>
<body>
<div>
<label><input type="radio" name="colorRadio" value="red"> red</label>
<label><input type="radio" name="colorRadio" value="green"> green</label>
<label><input type="radio" name="colorRadio" value="blue"> blue</label>
</div>
<div class="red box">You have selected <strong>red radio button</strong> so i am here</div>
<div class="green box">You have selected <strong>green radio button</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue radio button</strong> so i am here</div>
</body>
</html>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to bind click event to dynamically created HTML elements in jQuery
Use the jQuery on() method...How to check whether a value is numeric or not in jQuery
Use the jQuery.isNumeric() method You...How to sort an array values alphabetically in PHP
Use the PHP sort() and rs...How to Return Multiple Values from a Function in JavaScript
Return an Array of Values A function...How to Install FileZilla FTP client in Ubuntu
FileZilla is a open-source multi-platfor...