209

I have a radiobutton list and on click on the radio button item I have to change the text of its label. But for some reason it's not working. Code is below:

<asp:Label ID="lblVessel" Text="Vessel:" runat="server"></asp:Label>

<script language="javascript">
  $(document).ready(function() {

    $('#rblDiv input').click(function() {
      var selected = $("#rblDiv input:radio:checked").val();
      if (selected == "exportpack") {
        $('#lblVessel').text("NewText");
      }
    });
  });
</script>
double-beep
  • 5,031
  • 17
  • 33
  • 41
Amit
  • 6,839
  • 21
  • 56
  • 90
  • I'm not an ASP.NET guy but I think the id generated for the browser doesn't retain the ID you provided for it on server side. Do a view source and paste the relevant code here. – Allain Lalonde Aug 27 '10 at 12:38

10 Answers10

337

I was having the same problem because i was using

$("#LabelID").val("some value");

I learned that you can either use the provisional jquery method to clear it first then append:

$("#LabelID").empty();
$("#LabelID").append("some Text");

Or conventionaly, you could use:

$("#LabelID").text("some value");

OR

$("#LabelID").html("some value");
skolima
  • 31,963
  • 27
  • 115
  • 151
Chilli
  • 3,471
  • 2
  • 12
  • 4
  • I apply same but not working. I used child pages. How I write in child pages. I write `$('#contentPlaceHolderId_LabelID')` it not works. I also change th ClientMode to Static but no work. Please help. – Salman Mushtaq Feb 17 '16 at 12:52
  • I used `$('[id*=LabelID]').text("some value");` because my control was server control. – Ashi Feb 19 '20 at 06:33
  • This working: $("#LabelID").empty(); $("#LabelID").append("some Text"); thanks!!! – Ejrr1085 Oct 13 '22 at 01:44
59

ASP.Net automatically generates unique client IDs for server-side controls.

Change it to

 $('#<%= lblVessel.ClientID %>')

In ASP.Net 4.0, you could also set the ClientIDMode property to Static instead.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
49

Try this:

$('[id$=lblVessel]').text("NewText");

The id$= will match the elements that end with that text, which is how ASP.NET auto-generates IDs. You can make it safer using span[id=$=lblVessel] but usually this isn't necessary.

Codesleuth
  • 10,321
  • 8
  • 51
  • 71
  • I'm using asp.net and I was stuck until I saw this. Please anyone using asp.net, use this and not the others. [id$=lblVessel] is the trick. Thanks! – Matthew Zourelias Oct 19 '21 at 15:26
22

try this

$("label").html(your value); or $("label").text(your value);

Ehsan Mehralian
  • 325
  • 1
  • 3
  • 14
DCG Scuff
  • 221
  • 2
  • 2
5
<asp:RadioButtonList ID="rbtnType" runat="server">
    <asp:ListItem Value="C">Co</asp:ListItem>
    <asp:ListItem Value="I">In</asp:ListItem>
    <asp:ListItem Value="O">Out</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Label ID="lblLabelName" runat="server"></asp:Label>
<script type="text/javascript">
    $(document).ready(function() {
        $("#<%=rbtnType.ClientID%>").change(function() {
            var rbvalue = $("input[@name=<%=rbtnType.ClientID%>]:radio:checked").val();
            if (rbvalue == "C") {
                $('#<%=lblLabelName.ClientID %>').html('text1');
            } else if (rbvalue == "I") {
                $('#<%=lblLabelName.ClientID %>').html('else text2');
            } else if (rbvalue == "O") {
                $('#<%=lblLabelName.ClientID %>').html('or elsethistext');
            }
        });
    });
</script>
Sravan Kumar
  • 1,447
  • 3
  • 19
  • 41
5

we have to find label tag for attribute value based on that.we have replace label text.

Script:

<script type="text/javascript">
$(document).ready(function() 
{ 
$("label[for*='test']").html("others");
});

</script>

Html

<label for="test_992918d5-a2f4-4962-b644-bd7294cbf2e6_FillInButton">others</label>

You want to more details .Click Here

Tamilan
  • 67
  • 2
  • 3
5
   lable value $('#lablel_id').html(value);
suraj khot
  • 51
  • 1
  • 1
5

I just went through this myself and found the solution. See an ASP.NET label server control actually gets redered as a span (not an input), so using the .val() property to get/set will not work. Instead you must use the 'text' property on the span in conjuntion with using the controls .ClientID property. The following code will work:

$("#<%=lblVessel.ClientID %>").text('NewText');
atconway
  • 20,624
  • 30
  • 159
  • 229
3
$('#<%= lblVessel.ClientID %>').html('New Text');

ASP.net Label will be rendered as a span in the browser. so user 'html'.

Vikas Kottari
  • 495
  • 2
  • 10
  • 24
0

Try this in JS:

Clicking on the label text toggles it with an alternative text, while selecting the associated input field and giving the focus to the same associated input field.

<script type="text/javascript">"use strict";
  const oldLabelText="ORIGINAL BUTTON TAG TEXT";
  document.write(
    '<label id=labId for=inputId onClick="changeText(this.id)">'+oldLabelText+'</label>:<br>'+
    '<input id="inputId" value="The input value" />'
  );
</script>

<script type="text/javascript">"use strict";
  const stat=[oldLabelText,"NEW LABEL TEXT","THIRD LABEL TEXT","FOURTH LABEL TEXT"];
  let   cptr=0;

  function changeText(button_id){
     var el = document.getElementById(button_id);

     el.textContent = stat[++cptr % (stat.length)];

  // or
  //       el.firstChild.data = stat[++cptr % (stat.length)];

  // or
  //        el.innerHTML = stat[++cptr % (stat.length)];

  // or simply
  //        el.innerHTML = "the new text";
  }
</script>
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 04 '22 at 06:06