Paul Cann
Paul Cann

Reputation: 31

Setting an ASP Label control with text from JQuery

I'm new to JQuery and I'm writing code to check a registration form. Here's my label in the server page:

<asp:Label ID="LabelErrorFirstName" runat="server" ForeColor="Red" Font-Size="Small"></asp:Label>

and my JQuery script:

if(firstnameTxt.length == '') {
      $('#<%= LabelErrorFirstName.ClientID %>').text("Please enter a first name.");

  }  

I access this script from the OnClientClick event in a button on the form which I know works i.e. I set alerts here which shows it's getting there. I've commented out the 'if' statement in case that was the problem but it isn't.

What I want to happen is to have this label's text property set if the first name textbox is empty to "Please enter.." etc. I've tried many variations but I can't get it.

What am I missing?

Paul

Upvotes: 1

Views: 916

Answers (1)

Jono
Jono

Reputation: 4076

Length is an integer not a string.

if(firstnameTxt.length == 0) {
    $('#<%= LabelErrorFirstName.ClientID %>').text("Please enter a first name.");
} 

Also, make sure your label is being found by jquery:

if( $("#<%= LabelErrorFirstName.ClientID %>").length > 0 ) {
}

Upvotes: 1

Related Questions