How To Display Input Field OnClick Of Radio Button In JQuery

admin_img Posted By Bajarangi soft , Posted On 16-10-2020

In JQuery we can add input field on click of radio button .So today we discuss in this article about adding input field on click of button using jquery

How To Display Input Field OnClick Of Radio Button In JQuery

Step 1:Create index.html file and implement below code.
 

Category I  <input type="radio" name="answer" checked="checked" value="no"/> <br><br>
Category II <input type="radio" name="answer" value="yes"/> <br><br>
Other Category <input type="radio" name="answer" value="other"/><br>
<input style="display:none;" type="text" name="otherAnswer" id="otherAnswer"/>



Step 2:Implement jQuery to add new input field on click of radio button.
<script>
    $(document).ready(function(){
        $("input[type='radio']").change(function(){
            if($(this).val()=="other")
            {
                $("#otherAnswer").show();
            }
            else
            {
                $("#otherAnswer").hide();
            }
        });
    });
</script>
 

Complete Code For Displaying  Input Field OnClick Of Radio Button In JQuery
 

 
<!DOCTYPE html>
<html>
<head>
    <title>How To Display Input Field OnClick Of Radio Button In JQuery</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<style>
    body {
        background: black;
    }
</style>
<body>
<div class="container">
    <br><br><br>
    <div class="text-center">
        <h1 id="color" style="color: White;">Display Input Field OnClick Of Radio Button In JQuery</h1>
    </div>
    <br>
    <div class="col-md-2"></div>
    <div class="col-md-8">
        <div class="well">
            Category I  <input type="radio" name="answer" checked="checked" value="no"/> <br><br>
            Category II <input type="radio" name="answer" value="yes"/> <br><br>
            Other Category <input type="radio" name="answer" value="other"/><br>
            <input style="display:none;" type="text" name="otherAnswer" id="otherAnswer"/>
            <script>
                $(document).ready(function(){
                    $("input[type='radio']").change(function(){
                        if($(this).val()=="other")
                        {
                            $("#otherAnswer").show();
                        }
                        else
                        {
                            $("#otherAnswer").hide();
                        }
                    });
                });
            </script>
        </div>
    </div>
    <div class="col-md-2"></div>
</div>
</body>
</html>











 

Related Post