I am in desperate need of help. I’ve been looking everywhere for 2 days straight for an answer but no luck yet.
tldr: custom code that is supposed to format the text block into a currency, including decimals, is converting only one value instead of multiple ones. (code is from this template.)
I have created a CMS database for the purpose of generating invoices. I’ve assigned a field to “Prices”, added a Plain Text category (I can’t use the Number one since I need multiple values in the same field - which is not possible in the Number field).
After a few attempts I’ve succeeded in getting one of the value inside the text block to change into the desired result, but it only does it for the first one, and deletes the rest.
I’ve tried multiple ways to fix the code so that it recognises the rest of the values, but it just doesn’t work.
Would be grateful if you guys had some tips on how to solve this issue.
dk if anyone will ever need this, but if they do, well it’s here.
}
<style>
/* Set white-space to pre-line to preserve white-space and line breaks */
.NAME {
white-space: pre-line;
}
</style>
<script>
document.addEventListener("DOMContentLoaded", () => {
// Select all elements with the class "NAME"
const paragraphs = document.querySelectorAll(".NAME");
// Iterate over each paragraph element
paragraphs.forEach(paragraph => {
// Get the text content of the paragraph
let text = paragraph.textContent;
// Split the text into words
let words = text.split(/\s+/);
// Modify each word to add a "." after the second letter and add "£ " in front of each word
let modifiedWords = words.map(word => {
let modifiedWord;
if (word.length === 5) {
modifiedWord = word.slice(0, 2) + '.' + word.slice(2);
} else if (word.length >= 6) {
modifiedWord = word.slice(0, 3) + '.' + word.slice(3);
} else if (word.length === 4) {
modifiedWord = word.slice(0, 1) + '.' + word.slice(1);
} else {
modifiedWord = word;
}
return `£ ${modifiedWord}`;
});
// Join the modified words with <br> tags
let modifiedText = modifiedWords.join(' <br> ');
// Set the modified text back to the paragraph
paragraph.innerHTML = modifiedText;
});
});
</script>
it’s tailored for 6 digits max, if you need more, just mess around with the if (word.length === x)