views:

5690

answers:

4
var value = $("#text").val();

and value = 9.61

i need to convert 9.61 to 9:61 how can i use jquery replace function here...

pls help me

Regards Nisanth

+8  A: 

easy...

var value = $("#text").val()+""; // value = 9.61 use $("#text").text() if you are not on select box...
value.replace(".", ":"); // value = 9:61
Reigel
I tried that but an error occure as value.replace is not a function:(
Nisanth
thephpdeveloper added something to make it correct... that should do it... see above edited
Reigel
@thephpdeveloper thanks...
Reigel
Thanks Reigel it is now work for me :)
Nisanth
you're welcome....
Reigel
+5  A: 

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
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...
Reigel
A: 
(9.61 + "").replace('.',':')

or if your 9.61 is already string,

"9.61".replace('.',':')
S.Mark
A: 

Can be done with regular javascript function replace().

value.replace(".", ":");
Anders Eriksson
value here is not a string...
Reigel