Rainhider
Rainhider

Reputation: 836

Set Asp Label control's text value with jquery

This is in a jquery function in my master page: (tells me it cannot find lblDateFrom)

      $('#<%= lblDateFrom.ClientID %>').text(dateText);

This is in my aspx page:

        <asp:Label ID="lblDateFrom" CssClass="lblDateFrom" runat="server" Text="a date"></asp:Label>

The master page's jquery sees the div's I have created by class, but it will not see the label. I am attempting to change the text in page without a postback, but it won't find the label by class or id. Other ways I have tried that do not work:

      $(".lblDateFrom").text(dateText);
      $(".lblDateFrom").text = dateText;

can anyone get this to work? or know what the problem is?

Upvotes: 0

Views: 2907

Answers (2)

Rocky Zairil
Rocky Zairil

Reputation: 45

To get spesific label, you should use # id selector to pinpoint one particular tag. using $('.lblDateFrom') will select all tag with class lblDateFrom, which might not be your intention.

$(document).ready(function() {
  $('#lblDateFrom').text(dateText) ;
});

or using the label tag and id value, with starts with ^ or end with $ character if the id in format like id='lblDateFrom_0_xxx' or id='Client_0__lblDateFrom'

$(document).ready(function() {
  //pick label with id starts with lblDateFrom
  $('label [id^=lblDateFrom]').text(dateText);
  //or
  //pick label with id ends with lblDateFrom
  $('label [id$=lblDateFrom]').text(dateText);
});

Upvotes: 1

Rainhider
Rainhider

Reputation: 836

well, right after i posted, i found this works:

   $(".lblDateFrom").empty();
   $(".lblDateFrom").append(dateText);

Upvotes: 0

Related Questions