Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
89
rated 0 times [  90] [ 1]  / answers: 1 / hits: 72982  / 14 Years ago, tue, january 4, 2011, 12:00:00

I am simply trying to store a label in a variable in javascript but for some reason this isn't working with document.getElementById('control');. I know my javascript is linking to my html file fine because everything else works.



Here is my javascript code:



function performEvapCooledCircuit(txt)
{
var error = document.getElementById('lblError');


if (txt.value == null || isNaN(txt.value))
{
error.style.visibility = visible;
}
}


Here is the html code for my label:



<asp:Label ID=lblError class=NormLabel runat=server 
style=color:red; visibility:hidden; Text=Invalid Input.></asp:Label>


I am getting an error that says object expected??


More From » asp.net

 Answers
5

The ID that ASP.NET will generate will not be lblError so you'll need to reference it by its ClientID



document.getElementById('<%=lblError.ClientID %>');





If your javascript file is external I've usually had to write a type of Init javascript method to make sure my ID's were set up property



On your ASPX page:



<script type=text/javascript>
var lblError = null;
function InitializeVariables()
{
if (lblError == null) // make sure you only do this once
{
lblError = document.getElementById(<%=lblError.ClientID %>);
}
}
</script>
<asp:Label
ID=lblError
class=NormLabel
runat=server
style=color:red; visibility:hidden;
Text=Invalid Input.></asp:Label>


Then in your javascript file you'll have to call InitializeVariables() to make sure you've got the variables pointing to the proper asp.net controls



function performEvapCooledCircuit(txt)
{
InitializeVariables();

if (txt.value == null || isNaN(txt.value))
{
lblError.style.visibility = visible;
}
}

[#94382] Sunday, January 2, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ronans

Total Points: 460
Total Questions: 109
Total Answers: 108

Location: Slovenia
Member since Sat, Sep 11, 2021
3 Years ago
;