Change a unique Character on jQuery

March 25, 2018, at 07:19 AM

<span class="number1">10.00</span>

simply, i want to replace the '.'(dot) for a ','(comma) using jQuery.

I've tried several forms to search the $('.number1') characters and replacing it with a comma.

<span class="number1">10.00</span>

What if there is more than one Dot in the string?

Answer 1

Why use jQuery for such a simple operation? What you need is a simple string manipulation. Adding a library so that you can type a few less characters to do something so basic seems hardly worth it.

What you really need the the plain old JavaScript String.replace() method.

Here's jQuery and non-jQuery ways to do it:

// With jQuery: 
console.log($(".number1").text().replace(".", ",")); 
// Without jQuery: 
console.log(document.querySelector(".number1").textContent.replace(".", ",")); 
// When you need to replace all the . chars. in the string, you'll need to use 
// a regular expression with .replace().  
// The / / denote the delimiters of a regular expression 
// The \. is the escape code for a . 
// The g means do a global find/replace throughout the string 
// With jQuery: 
console.log($(".number1").text().replace(/\./g, ",")); 
// Without jQuery: 
console.log(document.querySelector(".number1").textContent.replace(/\./g, ","));
<script src=""></script> 
<span class="number1">10.00.00</span>

Answer 2

this is a solution in vanilla js:

let spanNumber = document.querySelector('.number1')
let number = spanNumber.textContent
let newNumber = number.split('.').join(',')
spanNumber.innerHTML = newNumber

short version with replace:

let DOMElement = document.querySelector('.number1')
let string = DOMElement.textContent.replace('.',',')
DOMElement.innerHTML = string
Answer 3

Just turn it to array by finding a dot delimiter with split method, then get it right back using join like:

 // Lets say that the value is
 var num1       = document.querySelector('.number1'); 
 num1.innerHTML = num1.innerHTML.split('.').join(','); // Outputs  10,05,53,324,343
Answer 4

Why don't you try to use more of the number libraries present on Javascript instead of going to "easy way" of replacing characters?

Perhaps you have other numbers on your content that you want to format. I suggest a read on NumberFormat doc:

But you can check a working example below that formats a number on US-EN format to pt-BR:

var el = document.querySelector('.number1');  
var value = parseFloat(el.textContent); 
var newValue = new Intl.NumberFormat('pt-BR', { minimumFractionDigits: 2 }).format(value); 
el.innerHTML = newValue;
<span class="number1">10.00</span>

Image selection in gridster jquery plugin

Image selection in gridster jquery plugin

On my website I use gridster widgetsWidgets have images on them which are retrieved from a remote location

tic tac toe game not working (javascript)

tic tac toe game not working (javascript)

I am working on a tic tac toe game, and I am at the point where the game decides who wins the gameI wrote the logic and all possible winner solutions, but when I play the game, the game announces a winner even when there is no winning combination

Collapse Google Vis Table When Row Content Hidden

Collapse Google Vis Table When Row Content Hidden

I have a spreadsheet where I am storing about 5000 rows across 25 columnsThere are specific category header rows and then subcategory rows

How to convert string into array object

How to convert string into array object

I have problem with converting string into array object