How do I do string replace in JavaScript to convert ‘9.61’ to ‘9:61’?

javascript, jquery

Solution

Do it like this:

var value = $("#text").val(); // value = 9.61 use $("#text").text() if you are not on select box...
value = value.replace(".", ":"); // value = 9:61
// can then use it as
$("#anothertext").val(value);

Updated to reflect to current version of jQuery. And also there are a lot of answers here that would best fit to any same situation as this. You, as a developer, need to know which is which.

Replace all occurrences

To replace multiple characters at a time use some thing like this: `name.replace(/&/g, "-")`. Here I am replacing all `&` chars with `-`. `g` means "global"

Note - you may need to add square brackets to avoid an error - `title.replace(/[+]/g, " ")`

credits vissu and Dante Cullari

Problem

Given the code line ``` var value = $("#text").val(); ``` and `value = 9.61`, I need to convert `9.61` to `9:61`. How can I use the JavaScript replace function here?

Original source

Related problems