274

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?

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
Nisanth Kumar
  • 5,667
  • 8
  • 33
  • 43
  • 29
    Note that this has nothing to do with jQuery, but just with plain old javascript. – Ikke Jan 27 '10 at 10:22
  • 1
    I have a similar problem. I have a 'var' variable in javascript and I want to replace the string/characters in it. I tried this code: var myObject="my%dynamic%value\n"; myObject=myObject.replace("%","").replace("\n",""); Also tried myObject=myObject.replace('%','').replace('\n',''); It gives me the error that replace is not a known function – Balaji Birajdar Nov 03 '11 at 10:33
  • The exact error message for the above issue is "Microsoft JScript runtime error: Object doesn't support property or method 'replace'" – Balaji Birajdar Nov 03 '11 at 10:38

8 Answers8

538

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
67

Probably the most elegant way of doing this is to do it in one step. See val().

$("#text").val(function(i, val) {
  return val.replace('.', ':');
});

compared to:

var val = $("#text").val();
$("#text").val(val.replace('.', ':'));

From the docs:

.val( function(index, value) )

function(index, value)A function returning the value to set.

This method is typically used to set the values of form fields. For <select multiple="multiple"> elements, multiple s can be selected by passing in an array.

The .val() method allows us to set the value by passing in a function. As of jQuery 1.4, the function is passed two arguments, the current element's index and its current value:

$('input:text.items').val(function(index, value) {
  return value + ' ' + this.className;
});

This example appends the string " items" to the text inputs' values.

This requires jQuery 1.4+.

cletus
  • 616,129
  • 168
  • 910
  • 942
41

I love jQuery's method chaining. Simply do...

    var value = $("#text").val().replace('.',':');

    //Or if you want to return the value:
    return $("#text").val().replace('.',':');
Shadrack Orina
  • 7,713
  • 2
  • 31
  • 36
  • 17
    this has nothing to do with jquery chaining actually. After you've called `.val()` it's a regular string. – zerkms Apr 16 '13 at 09:02
19

A simple one liner:

$("#text").val( $("#text").val().replace(".", ":") );
Marvin Pinto
  • 30,138
  • 7
  • 37
  • 54
VIDesignz
  • 4,703
  • 3
  • 25
  • 37
  • 1
    this will only work if the value is a string... and also, calling `$("#text")` twice in there is not a good practice... – Reigel Gallarde Dec 14 '12 at 00:51
  • the OP also didn't specify that he wanted to replace the value of the `$("#text")` :) – Reigel Gallarde Dec 14 '12 at 00:54
  • Comment 1...Wrong. Comment 2...think about the logic of his question, no need to specify. – VIDesignz Dec 14 '12 at 06:05
  • prove it.. and do remember, dated `Jan 27 '10 at 10:16` – Reigel Gallarde Dec 14 '12 at 06:12
  • you don't even know good practice.. you might want to read this.. http://stackoverflow.com/questions/3230727/jquery-optimization-best-practices – Reigel Gallarde Dec 14 '12 at 06:14
  • Why would you add an extra step by creating a variable first? Your link referred to using $this multiple times...I'm not seeing how this is "bad practice"...Remember the date? Um, ok...you answered the question over a year before I was even a member, I'm happy for you. – VIDesignz Dec 14 '12 at 06:28
  • I'm not sure why you wanted to argue... `.val()` before doesn't return a string... I'm not sure if you were reading the comments... and history of edits... – Reigel Gallarde Dec 14 '12 at 06:55
  • I don't want to argue anything, I am here to learn man. But false teaching doesn't teach. Check out this fiddle http://jsfiddle.net/videsignz/dH6J3/ as you can see `.val()` DOES return a string, even when the original value is a number. – VIDesignz Dec 14 '12 at 14:36
  • I'll give you that `.replace()` doesn't affect numbers, but `.val()` returns a string, hence why my answer works. No need to hack it with `$("#text").val()+""` – VIDesignz Dec 14 '12 at 14:41
  • 2010... `.val()` before doesn't return a string. – Reigel Gallarde Dec 14 '12 at 15:13
  • Ohhh...I see what you are saying, it didn't then, but does now? – VIDesignz Dec 14 '12 at 15:20
  • BUt if you knew that already, you wouldn't have said `this will only work if the value is a string` in your first comment ;) guess we both learned something today... – VIDesignz Dec 14 '12 at 15:24
  • here's one on the same year... http://stackoverflow.com/questions/2391274/jquery-replace-text-in-val yes, current version don't have that error... but I don't really do it like that... cletus' answer is the best one – Reigel Gallarde Dec 14 '12 at 15:27
  • Makes sense, I wasn't aware of the complications back then, I have only been working with Jquery for about a year...thanks for the clearing it up! – VIDesignz Dec 14 '12 at 15:34
6

It can be done with the regular JavaScript function replace().

value.replace(".", ":");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anders
  • 647
  • 2
  • 8
  • 16
  • 3
    Im not sure if I missed something but since the value comes from the textbox it is a string no? Example http://jsfiddle.net/xMBuA/ – Anders Dec 05 '12 at 13:31
4

You can use JavaScript functions like replace, and you can wrap the jQuery code in brackets:

var value = ($("#text").val()).replace(".", ":");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kravits88
  • 12,431
  • 1
  • 51
  • 53
3
$("#text").val(function(i,v) { 
   return v.replace(".", ":"); 
});
Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
1
(9.61 + "").replace('.',':')

Or if your 9.61 is already a string:

"9.61".replace('.',':')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
YOU
  • 120,166
  • 34
  • 186
  • 219