0

I want to replace the text( "My Text" ) that is inside of a tag <a>
this is my code:

<a id="myID" href="#">
    My Text
    <img src="/layout/images/blank.gif">
</a>

I tried:

$( "#myID" ).text( "New Text" );

But I returns this error:

TypeError: string is not a function
message: "string is not a function"
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
proto: Error

and:

$( "#myID" ).html( "New Text" );

TypeError: undefined is not a function
message: "undefined is not a function"
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
proto: Error

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
Lugarini
  • 792
  • 5
  • 11
  • 32

1 Answers1

1

You can use html() with callback function

$("#myID").html(function(i, v) {
    return v.replace("My Text", "New Text");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a id="myID" href="#">
    My Text
    <img src="/layout/images/blank.gif">
</a>
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188