I'm trying to replace parts of a Val() in jquery with some text but am not sure how to do this. $(this).val()
returns "# number of...." - I would like to replace #
with custom text, but Val doesn't seem to have a replace()
function. I'm quite new to JQuery, so I might be missing something obvious.
Thanks for any help
Asked
Active
Viewed 2.7k times
14

o-logn
- 500
- 1
- 10
- 21
2 Answers
23
You can pass a function to .val()
in 1.4+ like this:
$("#mySelector").val(function(i, v) { //index, current value
return v.replace("#","Custom Text");
});

Nick Craver
- 623,446
- 136
- 1,297
- 1,155
-
Perfect solution, Thank you +1 – Nimatullah Razmjo Feb 07 '16 at 08:00
1
Do not forget to add a .val() at the end :
$("#mySelector").val(function(i, v) {return v.replace("#","Custom Text");}).val();

Zitun
- 388
- 1
- 4
- 13