Javascript to erase text and disabled textbox?

3 posts, 1 answers
  1. Jesse
    Jesse avatar
    3 posts
    Member since:
    Jun 2014

    Posted 24 Jul 2014 Link to this post

    I am having trouble controlling a radtextbox. I have a radcombobox(ddlMaritalStatus) and a radtextbox(txtSpoueName) on my page. What I would like is that when a user selects "Single" from ddlMaritalStatus then txtSpouseName becomes disabled. My problem is that if a user selects "Married" and starts to type in txtSpouseName but then changes ddlMaritalStatus to "Single" the text is still in txtSpouseName. So how do I make it so that if user selects "Single" then txtSpouseName becomes disabled and any text is erased and when user selects "Married" txtSpouseName becomes enabled again?

    <p>
    <telerik:RadComboBox ID="ddlMaritalStatus" runat="server" EmptyMessage="--Select--" OnClientSelectedIndexChanged="DisableBox" TabIndex="15" AutoPostBack="false"></telerik:RadComboBox>
    ​<script type="text/javascript">

    function DisableBox(sender, args) {
        var TextBox = $find("<%=txtSpouseName.ClientID %>");
        if (sender.get_selectedItem().get_value() == "Single") {
             document.getElementById(sender.get_id().replace("ddlMaritalStatus", "txtSpouseName")).value = "";
             //document.getElementById("txtSpouseName").disabled=true;
             document.getElementById("txtSpouseName").setAttribute("Enabled", "False");
         }   
         else {
             document.getElementById(sender.get_id().replace("ddlMaritalStatus", "txtSpouseName")).value = "";
             //document.getElementById("txtSpouseName").disabled = false;
             document.getElementById("txtSpouseName").
         }
    }

    </script>
    <asp:RequiredFieldValidator ID="MaritalStatusValidator" runat="server" ErrorMessage="Please Select" ControlToValidate="ddlMaritalStatus"></asp:RequiredFieldValidator>
    </p>
    <p>
    <telerik:RadTextBox ID="txtSpouseName" runat="server" Enabled="false" AutoPostBack="false" TabIndex="16"></telerik:RadTextBox>
    <asp:RegularExpressionValidator ID="SpouseNameValidator" runat="server" ErrorMessage="First (Initial) Last" ValidationExpression="[A-Z][a-z]+ ?[A-Z]? ([A-Z][a-z]?')?([A-Z][a-z]+?-)?[A-Z][a-z]+" ControlToValidate="txtSpouseName"></asp:RegularExpressionValidator>
    </p>



    My script is not working
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 24 Jul 2014 in reply to Jesse Link to this post

    Hi Jesse,

    Please try the below JavaScript to achieve your scenario.

    JavaScript:
    function DisableBox(sender, args) {
        var TextBox = $find("<%=txtSpouseName.ClientID %>");
        if (args.get_item().get_text() == "Married") {
            TextBox.enable();
        }
        else {
            TextBox.clear();
            TextBox.disable();
        }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jesse
    Jesse avatar
    3 posts
    Member since:
    Jun 2014

    Posted 25 Jul 2014 in reply to Shinu Link to this post

    Thank you Shinu, this worked like a charm! I appreciate your help!
Back to Top