This is a migrated thread and some comments may be shown as answers.

Javascript to erase text and disabled textbox?

2 Answers 207 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jesse
Top achievements
Rank 1
Jesse asked on 24 Jul 2014, 01:52 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 25 Jul 2014, 02:58 AM
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.
0
Jesse
Top achievements
Rank 1
answered on 25 Jul 2014, 12:21 PM
Thank you Shinu, this worked like a charm! I appreciate your help!
Tags
General Discussions
Asked by
Jesse
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Jesse
Top achievements
Rank 1
Share this question
or