1

I have a label on a ASP.Net 4.0 website which I'm trying to hide when the users types into a textbox.

I have a Javascript function which I'm firing on the Keypressevent of a Textbox.

So, my Javascript function is:

function hideLabel(sender, e) {
    document.getElementById('<%=lblResult.ClientID%>').style.display = 'none';
}

And my textbox and labels are as follows:

<tr><td>
    <asp:Label ID="lblResult" runat="server" Visible="True"></asp:Label>
</td></td>
<tr><td>
  <asp:TextBox ID="txtEmailAddress" runat="server" Width="200px"
       CssClass="customtxt" onkeypress="hideLabel(this, event)"></asp:TextBox>
</td></td>

The function fires but I receive the following error: Microsoft JScript runtime error: Object required

I've also tried the below line of code within my function but this doesn't work either:

document.getElementById('lblResult').style.visibility = 'hidden';

Anybody got any idea why this is failing in IE 8 - it is working correctly in Chrome.

2
  • A couple of points: 1) With the label defined as you have done, it's actually being rendered as <span id="MainContent_lblResult">Text here</span> - which may not be what you intended - you should set the AssociatedControlID to the ID of the text box if you want a <label> instead. 2) Have you tried opening the IE Developer Tools (F12) and checked the Console for errors? The code as you have it should work, and I've tested a similar set up (albeit with IE9 in IE8 mode). Commented Feb 6, 2012 at 15:38
  • Your second JS attempt won't work: The control probably won't have the ID as you've written it (you're right to use the ClientID property), and Visibility is an ASP.NET property that doesn't exist in the DOM. Commented Feb 6, 2012 at 15:40

3 Answers 3

1

I think ,you do not need to pass any parameters to function in your case :

    function hideLabel() {
    document.getElementById("<%=lblResult.ClientID%>").style.display = "none";
}

<asp:TextBox ID="txtEmailAddress" runat="server" Width="200px" CssClass="customtxt" onkeypress="hideLabel()"></asp:TextBox>
Sign up to request clarification or add additional context in comments.

Comments

1

just method without parameters would work.

<script language="javascript" type="text/javascript">
    function hideIt() {
        document.getElementById('<%=myLabel.ClientID%>').style.display = 'none'; 
    }
</script>

UPDATED :

HTML My Label

SCRIPT function hideIt() { document.getElementById('<%=myLabel.ClientID%>').style.display = 'none'; }

I can confirm that it does work.... What browser are you using , so I can test it?

4 Comments

I'm using IE8 but I've just tried it in Google Chrome and you're right it does work there. Is there some setting on my IE? I have other keypress events running on different forms.
Here is my code, hope this pastes ok: function hideLabel() { alert("Here"); document.getElementById('<%=lblResult.ClientID%>').style.display = 'none'; } <asp:TextBox ID="txtEmailAddress" runat="server" Width="200px" CssClass="customtxt" onkeypress="hideLabel()"></asp:TextBox> I've tried the hideLabel call without the () but it still doesn't work. Also here is the label box: <asp:Label ID="lblResult" runat="server" Visible="True"></asp:Label>
can you render the page in browser and post the html source? that's got to be something to do with client ID.
OK, I've got this working now. I just had to check that the label wasn't null before trying this. After that it works. But thanks for all the help.
1

It's almost certainly failing b/c it's not finding the label you're looking for. Look at the generated source and make sure that the label is present on the page and that it's got the ID that is in the javascript generated output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.