Skip to content Skip to sidebar Skip to footer

Capitalize First Letter Using Javascript

I'm trying to capitalize first letter of sentence for every class. I have html multiple time on my page and I want to capitalize first lette

Solution 1:

Why not stick with jQuery, as you're already using it

$('.price').text(function(_, txt) {
    return txt.charAt(0).toUpperCase() + txt.slice(1).toLowerCase();
});

Solution 2:

You can do it with CSS:

.price:first-letter{
   text-transform: capitalize;
}

Solution 3:

Iam not sure what you get with your document.getElementsByClassName('price'); but this work it will return Toto

function applySentenceCase() 
{
   var selector = "toto";
   return selector.charAt(0).toUpperCase() + selector.substr(1).toLowerCase();
}

Solution 4:

Your function is basically right. I imagine there is a problem with the jquery? I changed it a tiny bit to use javascript. https://jsfiddle.net/h6h4nswd/

var selectorTest = selector[i].innerHTML;

Post a Comment for "Capitalize First Letter Using Javascript"