jqueryasp.net

How to replace anchor text inside ASP.net label?


How to Replace anchor tag inner text inside of Asp.net label:

For Example :

<asp:Label ID="AER_UI_Exam_MPN_ThreeAttemptsBefore_1" runat="server" Text="Please visist: <a id='google'class='translatetext lblfont' href='https://google.com' target='_blank' style="font-size: 12px;">google</a>" CssClass="lblfont translatetext" Style="font-size: 12px;" />


                                           

if I will get the id based on class

$(".translatetext").each(function () {
var id = this.id;
 dictionary[id] = $("#" + id + "")[0].innerText;
});

I am getting both the id but anchor tag is replacing as text not as link. here while debug I found the that for first id -innertext is:Please visist:google, when this inner text is replacing with

 "some text" 

the second id is not found for anchor. how to handle this scenario?

Result is: Please visist:google,

Expected Result: :Please visist:[google]


Solution

  • Hi please try this.

      <asp:Label ID="AER_UI_Exam_MPN_ThreeAttemptsBefore_1" runat="server" CssClass="lblfont translatetext" 
                Style="font-size: 12px;"> Please visist: <a id='google'class='translatetext lblfont' href='https://google.com' target='_blank' style="font-size: 12px;">google</a></asp:Label>
    

    Edited :

    First you build the anchor tag in Jquery. Like this.

     var myhtml="Please visist: <a id='google' class='translatetext lblfont' href='https://google.com'
    
     target='_blank' style='font-size: 12px;'>"+some text+"</a>";
    

    then assign the above variable to that label.

    $("#AER_UI_Exam_MPN_ThreeAttemptsBefore_1").html(myhtml);