How to enable textbox in javascript on button click

  • How to enable disable textbox when radio button selected using javascript and Jquery
  • Example-1. Enable Disable Textbox based on Radio Button selection using Jquery
    • HTML Code
    • Jquery Code
  • Example-2. Enable Disable Textbox based on Radio Button selection using Javascript
    • HTML Code
    • Javascript Code
  • Conclusion
  • See Also
    • Download Javascript form validation
    • How to show and hide div elements based on dropdown selection

How to enable disable textbox when radio button selected using javascript and Jquery

In this tutorial, we will explain How to enable disable textbox when radio button selected using javascript and jquery with a live example.

While designing the HTML form some times we require an option to enable disable textbox field when the user clicks or select the radio button.

Here in this example, We will create a simple HTML form with 3 radio buttons and one textbox field and using jQuery the click() function to enable disable the textbox field.

Example-1. Enable Disable Textbox based on Radio Button selection using Jquery

How to enable textbox in javascript on button click

HTML Code

The below HTML codes to create a simple form with radio buttons and Textbox.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<div class="col-md-6 border rounded shadow-sm">

        <form id="signupForm"method="get"action="">

          <div class="form-row">

            <div class="form-group col-md-2">

              <input type="radio" id="english"name="subject"value="english"checked>

              English</div>

            <div class="form-group col-md-2">

              <input type="radio"id="hindi"name="subject"value="hindi">

              Hindi </div>

            <div class="form-group col-md-5">

              <input type="radio" id="other"name="subject"value="other"class="otherLang">

              Other Language</div>

          </div>

          <div class="form-row">

            <div class="form-group col-md-3">

              <label>Other Language</label>

            </div>

            <div class="form-group col-md-6">

              <input type="text"id="otherlang" name="otherLanguage"disabled="disabled"  placeholder="Other Language">

            </div>

          </div>

        </form>

      </div>

Jquery Code

In the below code the Jquery click function will enable disable the textbox field on radio button selection.

Download Jquery Library

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script type="text/javascript">

   $("input[name='subject']").click(function(){

      $("#otherlang").prop("disabled",true);

       $('#otherlang').val("");

      if($(this).hasClass('otherLang')){

      $("#otherlang").prop("disabled",false);

      }

   });

    </script><gwmw style="display:none;"><gwmw style="display:none;"></gwmw></gwmw>

How to enable textbox in javascript on button click

Example-2. Enable Disable Textbox based on Radio Button selection using Javascript

How to enable textbox in javascript on button click

HTML Code

The following HTML codes used to creating the simple HTML form.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<div class="col-md-6 border rounded shadow-sm">

        <form  method="get"action="">

          <div class="form-row">

            <div class="form-group col-md-2">

              <input type="radio" id="eng"name="subject"value="english"checked onclick="EnableDisableTB()">

              English</div>

            <div class="form-group col-md-2">

              <input type="radio"id="hin" name="subject"value="hindi"onclick="EnableDisableTB()">

              Hindi</div>

            <div class="form-group col-md-5">

              <input type="radio"id="others"name="subject"value="other"  onclick="EnableDisableTB()">

              Other Language</div>

          </div>

          <div class="form-row">

            <div class="form-group col-md-3">

              <label>Other Language</label>

            </div>

            <div class="form-group col-md-6">

              <input type="text"id="otherlan"name="otherLanguage"disabled="disabled"  placeholder="Other Language">

            </div>

          </div>

        </form>

      </div>

Javascript Code

The following javascript code is used to enable, disable the textbox based on the radio button is selected (checked) or unselected (unchecked)

<script type="text/javascript">

    functionEnableDisableTB(){

        varothers= document.getElementById("others");

        varotherlan=document.getElementById("otherlan");

        otherlan.disabled =others.checked?false:true;

        otherlan.value="";

        if (!otherlan.disabled){

            otherlan.focus();

        }

    }

</script>

How to enable textbox in javascript on button click

Conclusion

We explained How to enable disable textbox when radio button selected or checked using jQuery and Javascript. Hope you like it so please like and share.

See Also

Download Javascript form validation

How to show and hide div elements based on dropdown selection

How do I GREY out a TextBox in JavaScript?

Javascript: tools. eraser = function () { var tool = this; this. started = false; var varPenColor = "White"; context.

How do I disable input on click?

prop('disabled', true); Will do. But keep in mind after you disable it (by the above code) onclick handler wont work as its disabled. To enable it again add $("#FullName").

What is TextBox in JavaScript?

JavaScript TextBox - Modern Text Field with Floating Label. An extended version of the HTML input element that supports both pure-CSS and pure-JavaScript versions. Easily create input groups with icons, buttons, help text, and validation messages.

How do I disable a TextBox?

We can easily disable input box(textbox,textarea) using disable attribute to “disabled”. $('elementname'). attr('disabled','disabled'); To enable disabled element we need to remove “disabled” attribute from this element.