I have a string which has a number in it that I would like to replace with another number.
ie
<a href="" id="my_link">blah blah 32 blah blah</a>
I know there is only going to be 1 number in this string.
I can get this far:
var my_string = $('a#my_link').text();
But basically I don’t know how to then perform a search on my_string for a numeral and replace that number with something else.
Is that possible with jQuery?
Thanks for any ideas.
Many jQuery methods like
.text()can accept a function that returns the value to insert.Try it out: http://jsfiddle.net/6mBeQ/
This removes the need to run the selector twice.
Also, when you are getting an element by its ID, it is actually a little quicker if you do not include the tag name.
So instead of
it is better to do
as I did above.