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?

3
  • 29
    Note that this has nothing to do with jQuery, but just with plain old javascript. Commented Jan 27, 2010 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 Commented Nov 3, 2011 at 10:33
  • The exact error message for the above issue is "Microsoft JScript runtime error: Object doesn't support property or method 'replace'" Commented Nov 3, 2011 at 10:38

8 Answers 8

535

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

Sign up to request clarification or add additional context in comments.

6 Comments

I tried that but an error occure as value.replace is not a function :(
thephpdeveloper added something to make it correct... that should do it... see above edited
This won't work if we try to replace more than one value at a time. :(
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, " ")
|
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+.

1 Comment

but as i see it the guy just want to change the var value and not the value in the text box as you suggested...
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('.',':');

1 Comment

this has nothing to do with jquery chaining actually. After you've called .val() it's a regular string.
19

A simple one liner:

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

14 Comments

this will only work if the value is a string... and also, calling $("#text") twice in there is not a good practice...
the OP also didn't specify that he wanted to replace the value of the $("#text") :)
Comment 1...Wrong. Comment 2...think about the logic of his question, no need to specify.
prove it.. and do remember, dated Jan 27 '10 at 10:16
you don't even know good practice.. you might want to read this.. stackoverflow.com/questions/3230727/…
|
6

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

value.replace(".", ":");

1 Comment

Im not sure if I missed something but since the value comes from the textbox it is a string no? Example jsfiddle.net/xMBuA
4

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

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

Comments

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

Comments

1
(9.61 + "").replace('.',':')

Or if your 9.61 is already a string:

"9.61".replace('.',':')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.