Is it possible to retrieve an element by its ID using JavaScript when there is no form tag available?
I am trying to obtain the value of a textbox with the following command, but I keep receiving a 'null or undefined' error message. It works correctly when there is a proper form tag in an ASP.NET page.
var _txt = document.getElementById("txt").value;
Are there alternative methods to access textbox values in JavaScript when there are no form, body, and header tags on an ASP.NET page?
Below is the code snippet:
<asp:dropdownlist runat="server" ID="ddl" Height="20px"
Width="215px" ValidationGroup="aa">
<asp:ListItem Selected="True" Value="0">--select--</asp:ListItem>
<asp:ListItem Value="1">item-1</asp:ListItem>
</asp:dropdownlist>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="ddl" ErrorMessage="required" InitialValue="0"
SetFocusOnError="True" Display="Dynamic" ValidationGroup="aa"></asp:RequiredFieldValidator>
<br />
<asp:textbox runat="server" ID="txt" ValidationGroup="aa" Width="140px"></asp:textbox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txt" ErrorMessage="required" InitialValue=""
SetFocusOnError="True" Display="Dynamic" ValidationGroup="aa"></asp:RequiredFieldValidator>
<br />
</div>
<div>
<asp:validationsummary runat="server" ID="valSummary"
HeaderText="Vendor Ref and Contact are required" ValidationGroup="aa"
CssClass="valsummary">
</asp:validationsummary>
</div>
<asp:Button ID="btn" runat="server" Text="DO" ValidationGroup="aa"
Width="118px" OnClientClick="Test2()"/>
<br />
<script type="text/javascript" language="javascript">
function Test() {
var _indx = document.getElementById('ddl');
var _txt = document.getElementById("txt").value;
if (_indx.selectedIndex == 0 || _txt == '') {
document.getElementById('_div').style.visibility = 'visible';
document.getElementById('_div').style.display = "block";
}
else {
document.getElementById('_div').style.visibility = 'hidden';
document.getElementById('_div').style.display = "none";
}
}
function Test2() {
var _indx = document.getElementById('ddl');
var _txt = document.getElementById("txt").value;
var msg_I = "........"
var msg_II = "...."
var msg_III = ".."
if (_indx.selectedIndex == 0 && _txt == '') {
txtHeader = msg_I
}
else if (_indx.selectedIndex == 0 && _txt != '') {
txtHeader = msg_III
}
else if (_indx.selectedIndex != 0 && _txt == '') {
txtHeader = msg_II
}
document.getElementById('<%= valSummary.ClientID %>').headertext = txtHeader;
}
</script>