We help IT Professionals succeed at work.
Get Started

use javascript to make a textbox visible on DDL selection

maqskywalker
maqskywalker asked
on
410 Views
Last Modified: 2015-03-03
I'm using Visual studio 2013. I have a regulare .aspx web forms page.

So on my page i have a asp DropDownList and a ASP Textbox.

This is my code:

TestPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DDL_ASP_EnableDisableTextBox1.aspx.cs" Inherits="Wizards2.DDL_ASP_EnableDisableTextBox1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Make TextBox Visible on DDL Selection</title>

    <script type="text/javascript">

        // disable textbox by default
        //Textbox1.disabled = true;
        document.getElementById('TextBox1').style.visibility = 'hidden';

        function Enable() {

            document.getElementById('TextBox1').style.visibility = 'visible';
            //document.getElementById("TextBox1").disabled = false;

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDown_Fruit" runat="server" AutoPostBack="True" onchange="Enable();">
            <asp:ListItem>Apple</asp:ListItem>
            <asp:ListItem>Banana</asp:ListItem>
            <asp:ListItem>Pear</asp:ListItem>
            <asp:ListItem>Watermelon</asp:ListItem>
        </asp:DropDownList>
        <br /><br /><br /><br />                  
        <asp:TextBox runat="server" ID="TextBox1" />       

    </div>
    </form>
</body>
</html>

Open in new window


This is what I want:

When the page launches I want TextBox1 to be hidden.
Then if I select any item from the Drop Down List then I want the textbox to become visible.


Where am I making a mistake in my code above?
Comment
Watch Question
Web Ninja at large
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE