2
Basing myself in that question, I adapted a specific answer for my case, creating an array with the paragraphs of interest, where my intention is to capitalize the text present in these respective paragraphs (with some exceptions, such as abbreviations, prepositions and Roman numbers).
The code works perfectly, however I can’t get the capitalized results to replace the previous ones (before capitalization) in the HTML body. I’ve already removed the line console.log
, replacing it with a capitalize(innerHTMLs);
- that works when I see the result on the console, but I’m not able to make that mentioned replacement on the page itself.
One thing I found out is that using document.getElementsByClassName('h6').innerHTML = 'Novo valor'
does not work unless I use an ID instead of class.
Any idea how I can replace paragraph text with classes h6
and professor
their values capitalised in the function capitalize()
? In other words:
<p class="h6">PREPOSIÇÃO DA SILVA</p>
transform into
<p class="h6">Preposição da Silva</p>
This is the code I’m using:
var innerHTMLs = Array.prototype.slice.call(document.querySelectorAll("[class='h6'], [class='professor']")).map(function(x){return x.innerHTML});
function abreviacao(s) {
return /^([A-Z]\.)+$/.test(s);
}
function numeralRomano(s) {
return /^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/.test(s);
}
function capitalize(texto) {
let prepos = ["da", "do", "das", "dos", "e", "de", "à", "ao", "às", "aos", "tfc" ];
return texto.split(' ') // quebra o texto em palavras
.map((palavra) => { // para cada palavra
if (abreviacao(palavra) || numeralRomano(palavra)) {
return palavra;
}
palavra = palavra.toLowerCase();
if (prepos.includes(palavra)) {
return palavra;
}
return palavra.charAt(0).toUpperCase() + palavra.slice(1);
})
.join(' '); // junta as palavras novamente
}
innerHTMLs.forEach(t => console.log(capitalize(t)));
<p class="h6">PREPOSIÇÃO DA SILVA</p>
<p class="professor">ROMANOS IX</p>
<p class="h6">PREPOSIÇÃO DOS SANTOS</p>
<p class="professor">JOÃO PAULO II</p>
<p class="h6">INTRODUÇÃO À PRIMEIRA CARTA AOS CORÍNTIOS</p>
<p class="professor">REFERENTE AO PARÁGRAFO IV</p>
Its function
capitalize
only returns the text, does not change at any point, to exchange it is necessary to reference the element, but its variableinnerHTMLs
only contains "texts" (actually Htmls), not references to the elements, you need to change to save them.getElementsByClassName
returns an array of elements, not an element, so the.innerHTML
it won’t work, you’d have to iterate it to do something likeelements[0].innerHTML = capitalize(innerHTMLs[0])
,elements[1].innerHTML = capitalize(innerHTMLs[1])
, ...– Costamilam
@First, thank you for the comment! I had already tried to do what you suggested to me, using
innerHTMLs[0].innerHTML = capitalize(innerHTMLs[0])
, but without success. In fact, there is capitalization in the element I refer to, but there is no substitution within the page.– winiercape
Even I had created a bond
for(var i = 0; i < innerHTMLs.length; i++){innerHTMLs[i].innerHTML = capitalize(innerHTMLs[i])}
, but, as I said, unsuccessfully in replacing.– winiercape
That’s because
innerHTMLs[i]
is not a reference to the HTML element, but HTML inside the element as a string. As I said, you need to adapt your variableinnerHTMLs
to store the element, not its contents (.innerHTML
)– Costamilam
@Costamilam I get it. I will search how to do this (save the element - text - instead of content). Thank you!
– winiercape