Enable TextBox if other option is selected in DropDownlist using JavaScript and jQuery

Introduction: Hello Guys, “Welcome back” Today i am here with another one new great article. In this article I will explain about how to activate or enable the TextBox if other option is selected by the user from DropDownlist using JavaScript and jQuery. When user will select Other (option) from the DropDownList is selected, the TextBox will be enabled else disabled.

Activate TextBox if another option is selected in DropDownBox using JavaScript

On the HTML page I will place DropDownList by using (HTML SELECT Tag) and a TextBox. To the DropDownList I will assign a JavaScript OnChange event handler. When user will select the item (option) from the dropdownlist, then the EnableDisableTextBox JavaScript function will be executed. Inside this function, based on whether the Other option (item) is selected, the TextBox will be enabled else disabled.

<script type="text/javascript">

    function EnableDisableTextBox(ddlModels) {

        var selectedValue = ddlModels.options[ddlModels.selectedIndex].value;

        var txtOther = document.getElementById("txt_other");

        txtOther.disabled = selectedValue == 5 ? false : true;

        if (!txtOther.disabled) {

            txtOther.focus();

        }

    }

</script>

<span>Your City?</span>

<select id = "ddlModels" onchange = "EnableDisableTextBox(this)">

       <option value="1">Chandigarh</option>

    <option value="2">New Delhi</option>

    <option value="3">Batala</option>

    <option value="4">Amritsar</option>

    <option value="5">Other</option>

</select>

<br />

<br />

Other:

<input type="text" id="txt_other" disabled="disabled" />

Activate TextBox if another option is selected in DropDownBox using jQuery

On the HTML page I will place DropDownList by using (HTML SELECT Tag) and a TextBox.  To the DropDownList I will assign a jQuery Change event handler.

Inside this function, based on whether the Other option (item) is selected, the TextBox will be enabled else disabled.

On the HTML page I will place DropDownList by using (HTML SELECT Tag) and a

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script type="text/javascript">

    $(function () {

        $("#ddlModels").change(function () {

            if ($(this).val() == 5) {

                $("#txt_other").removeAttr("disabled");

                $("#txt_other").focus();

            } else {

                $("#txt_other").attr("disabled", "disabled");

            }

        });

    });

</script>

<span>Your City</span>

<select id="ddlModels">

    <option value="1">Chandigarh</option>

    <option value="2">New Delhi</option>

    <option value="3">Batala</option>

    <option value="4">Amritsar</option>

    <option value="5">Other</option>

</select>

<br />

<br />

Other:

<input type="text" id="txt_other" disabled="disabled" />

Here I am giving complete code for HTML page with JavaScript function. You need to copy and paste the code in your HTML page for enjoying the code.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

    <style type="text/css">

        body

        {

            font-family: Arial;

            font-size: 10pt;

        }

    </style>

</head>

<body>

    <script type="text/javascript">

        function EnableDisableTextBox(ddlModels) {

            var selectedValue = ddlModels.options[ddlModels.selectedIndex].value;

            var txtOther = document.getElementById("txt_other");

            txtOther.disabled = selectedValue == 5 ? false : true;

            if (!txtOther.disabled) {

                txtOther.focus();

            }

        }

    </script>

    <span> Select Your City </span>

    <select id="ddlModels" onchange="EnableDisableTextBox(this)">

    <option value="1">Chandigarh</option>

    <option value="2">New Delhi</option>

    <option value="3">Batala</option>

    <option value="4">Amritsar</option>

    <option value="5">Other</option>

 

    </select>

    <br />

    <br />

    Other:

    <input type="text" id="txt_other" disabled="disabled" />

</body>

</html>

Here I am giving complete code for HTML page with jQuery function. You need to copy and paste the code in your HTML page for enjoying the code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#ddlModels").change(function () {
                if ($(this).val() == 5) {
                    $("#txt_other").removeAttr("disabled");
                    $("#txt_other").focus();
                } else {
                    $("#txt_other").attr("disabled", "disabled");
                }
            });
        });
    </script>
    <span>Select Your City</span>
    <select id="ddlModels">
            <option value="1">Chandigarh</option>
    <option value="2">New Delhi</option>
    <option value="3">Batala</option>
    <option value="4">Amritsar</option>
    <option value="5">Other</option>
 
    </select>
    <br />
    <br />
    Other:
    <input type="text" id="txt_other" disabled="disabled" />
</body>
</html>

Conclusion: In above code, I have been explained that how to activate or enable the TextBox if other option is selected by the user from DropDownlist using JavaScript and jQuery. Bye Bye and take care of yourself Developers. We will come back shortly with the new article.

 

Regards

Using Asp.net

Comments

Popular posts from this blog

Sending reset password link for one time use only in asp.net

add delete update inside gridview using store procedure in ASP.NET

Change password using asp.net