Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<asp:TextBox ID="txtid" runat="server">


<asp:DropDownList ID="ddlid" runat="server" onClick="javascript:showReadonly('<%=txtid.ClientID%>',this.value);">
<asp:ListItem Text="Select" />
<asp:ListItem Text="English" />
<asp:ListItem Text="Other Course" />



I have one textbox and one dropdown list ,dropdownlist have listitems select,English and other course.
when i select other course in the dropdownlist i want to enable textbox,other cases i want disabled textbox.
I written following code it is not working.
plz give any suggetions for me.

Note:Here document.getElementbyID is not identify textbox ID so It will show error message like document.getElementbyID(..) is null or not an object
How can solve this problem plz help me.

<script type="text/javascript">
function showReadonly(txtobj, valobj)
{
if (valobj == "Other Course")
{
document.getElementById(txtobj).disabled = false;
document.getElementById(txtobj).focus();
}
else {
document.getElementById(txtobj).disabled= true;

}
</script>


Thanks&egards
venkat
Posted
Updated 16-Nov-11 19:03pm
v6
Comments
[no name] 16-Nov-11 5:15am    
1) format your code snippets

2) give more information than "is not working". What isn't working? What is happening?

Check your textbox id's at runtime and replace it with that one...

Actually javascript not able to find that textbox.
 
Share this answer
 
Hi,

1. Check whether the javascript function is able to find the textbox or not
2. See any error that IE is reporting
3. Put a breakpoint and debug the javascript.

I hopw you will find what exactly the error

As I know it not able to find the textbox control through javascript thats why it is unable to disable it.

Hope this helps
 
Share this answer
 
Please check this blog

javascript-for-enableddisabled
 
Share this answer
 
Hi Venkat,

Check the below code. I've tested it in IE8.

JS code for thiss solution is as follows
XML
<script type="text/javascript">
    function showReadonly(valobj) {
        var txtobj = document.getElementById("<%= txtid.ClientID %>");

        if (valobj == "Other Course") {
            txtobj.disabled = false;
            // If you want to display the control
            txtobj.style.display = 'inline';
            txtobj.value = '';
            txtobj.focus();
        }
        else {
            txtobj.disabled = true;
            // If you want to hide the control
            txtobj.style.display = 'none';
            txtobj.value = '';
        }
    }
</script>


HTML for that should be as follows
XML
<asp:TextBox ID="txtid" runat="server"></asp:TextBox>
<asp:DropDownList ID="ddlid" runat="server" onChange="javascript:showReadonly(this.value);">
    <asp:ListItem Text="Select" />
    <asp:ListItem Text="English" />
    <asp:ListItem Text="Other Course" />
</asp:DropDownList>


Try this way.

Thank you,
Vamsi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900