byte1
asked on
Javascript dropdownlist hide/unhide textbox
How do i control a textbox from a dropdownlist value. I need to make a text box visible when a particular vlaue is selected in the dropdownlist box. I basically want to avoid a post back to control this.
A simple sample:
<form id="form1" runat="server">
<script language="javascript" type="text/javascript">
function Verify()
{
var element = document.getElementById('ddlValues');
if (element.value == 'Value2')
document.getElementById('txtTest').style.visibility = 'hidden';
else
document.getElementById('txtTest').style.visibility = 'visible';
}
</script>
<div>
<asp:DropDownList ID="ddlValues" runat="server" onchange="Verify();">
<asp:ListItem Text="Value 1" Value="Value1"></asp:ListItem>
<asp:ListItem Text="Value 2" Value="Value2"></asp:ListItem>
<asp:ListItem Text="Value 3" Value="Value3"></asp:ListItem>
<asp:ListItem Text="Value 4" Value="Value4"></asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
</div>
</form>
If you don't know how to read the value, Please see below function:
function displayTextBox()
{
var txtbox = document.getElementById('" + txtbox.UniqueID + @"');
var ddList = document.getElementById('" + ddlist.UniqueID + @"').value;
if(ddList == 'E')
{
lblEmp.style.display = '';
txt.style.display = '';
}
else
{
txtbox.style.display = 'none';
}
}
Call This Function onchange="displayOffer();" of DDL.
Good Luck.
Let me know if you have any question.
function displayTextBox()
{
var txtbox = document.getElementById('"
var ddList = document.getElementById('"
if(ddList == 'E')
{
lblEmp.style.display = '';
txt.style.display = '';
}
else
{
txtbox.style.display = 'none';
}
}
Call This Function onchange="displayOffer();"
Good Luck.
Let me know if you have any question.
ASKER
jorge I tried your solution, but get this error
0.
Message: Object required
Line: 111
Char: 9
Code: 0
0.
Message: Object required
Line: 111
Char: 9
Code: 0
That error due to, your code not able to find out the controls. Did you try my code? My code will run 100%. I have tested on my local system.
Check your html source code... does the controls names are the same as the code I put?
ASKER
gdupadhyay, What would be the UniqueID ? I still get the same error. i have also removed the 'lblEmp' line
ASKER
jorge, yes .i have copied my exact code.
function VerifyCoreProcessor()
{
var element = document.getElementById('d dlCoreProc essor');
if (element.value == 'Other')
document.getElementById('t xtCoreProc essor').st yle.visibi lity = 'hidden';
else
document.getElementById('t xtCoreProc essor').st yle.visibi lity = 'visible';
}
<asp:dropdownlist id="ddlCoreProcessor" runat="server" Width="128px" onchange="VerifyCoreProces sor();">
..........................
..........................
However I get a tag on "onchange" which is :"Could not find any attribute 'onchange' of element _
'dropdownlist'
function VerifyCoreProcessor()
{
var element = document.getElementById('d
if (element.value == 'Other')
document.getElementById('t
else
document.getElementById('t
}
<asp:dropdownlist id="ddlCoreProcessor" runat="server" Width="128px" onchange="VerifyCoreProces
..........................
..........................
However I get a tag on "onchange" which is :"Could not find any attribute 'onchange' of element _
'dropdownlist'
There is no problem with that tag... is warning, the IDE do not recognize at design time that property of the control
I meant with "source code" when you run your application and see the source code inside the browser
I meant with "source code" when you run your application and see the source code inside the browser
ASKER
i checked this time, it seems to be prefixing a tax to my control names. like
<TD><select name="_ctl14:ddlCoreProces sor" id="_ctl14_ddlCoreProcesso r"
<TD><select name="_ctl14:ddlCoreProces
UniqueID is used to read the ID of server controls. My function is read the selected value of DDL and compair the selected value. If the selected value is 'E', display the controls other wise don't display.
Can you please send me the code and .aspx file. It will help me to give you exact code.
Can you please send me the code and .aspx file. It will help me to give you exact code.
Check with this:
<form id="form1" runat="server">
<script language="javascript" type="text/javascript">
function Verify()
{
var element = document.getElementById('_ctl14:ddlCoreProcessor');
if (element.value == 'Value2')
document.getElementById('_ctl14:txtTest').style.visibility = 'hidden';
else
document.getElementById('_ctl14:txtTest').style.visibility = 'visible';
}
</script>
<div>
<asp:DropDownList ID="ddlValues" runat="server" onchange="Verify();">
<asp:ListItem Text="Value 1" Value="Value1"></asp:ListItem>
<asp:ListItem Text="Value 2" Value="Value2"></asp:ListItem>
<asp:ListItem Text="Value 3" Value="Value3"></asp:ListItem>
<asp:ListItem Text="Value 4" Value="Value4"></asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
</div>
</form>
The only thing that we must check is the id of your textbox in the source code of the browser
ASKER
Text box id :
</select><input name="_ctl14:txtCoreProces sor" type="text" id="_ctl14_txtCoreProcesso r" /></TD>
do i need to use a HTML textbox ?
</select><input name="_ctl14:txtCoreProces
do i need to use a HTML textbox ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can use
txtbox.style.display = 'none';
or
txtbox.style.display = '';
Depened on selected value of DDL.
Please let me know if you have any question