2

I want to update the letter case in the output. I am having this type of input string:

Input:

<style>
p {
    text-transform: lowercase;
}

p:first-letter {
    text-transform: uppercase;
}
</style>


<p>HELLO I AM HERE</p>

Current Output

Hello i am here

Desired Output:

Hello I Am Here 

The CSS is not working for me.

Pugazh
  • 9,453
  • 5
  • 33
  • 54
steven
  • 127
  • 1
  • 3
  • 8

4 Answers4

2

<p>HELLO I AM HERE</p>

Since your string is all capitalized, you can't really do it with CSS alone without tedious wrapping of each word in a <span> tag, but JS can do the trick:

var nodes = document.querySelectorAll('.test');

Array.prototype.forEach.call(nodes, function(node) {
  node.textContent = node.textContent.split(' ').map(function(word) {
    return word[0].toUpperCase() + word.slice(1).toLowerCase();
  }).join(' ');
});
<p class='test'>HELLO I AM HERE</p>
<p class='test'>HELLO I AM HERE AGAIN</p>
<p class='test'>HELLO I AM HERE AGAIN AND AGAIN</p>
timolawl
  • 5,434
  • 13
  • 29
0
text-transform: capitalize

Is what you want.

Edit: if your initial string is all uppercase you may have to make it lowercase with javascript first...

Derek
  • 1,826
  • 18
  • 25
0

Well, this is a straight answer:

pass input text into getCapitalize below javascript function and it will return you expected output-

var text = $(".capitalize").text();

var formattedText = getCapitalize(text); // use of function

$(".capitalize").text(formattedText);  // assign formatted text the container


 
function getCapitalize(text){  //formatter function
  
     var textArray = text.split(" ");

     for(i= 0; i<textArray.length; i++){
  
     textArray[i] = textArray[i][0].toUpperCase() + textArray[i].substr(1).toLowerCase();

     }
     return textArray.join(" ");      
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="capitalize">HELLO I AM HERE.</p>

Hoping this will help you:)

Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
0

You can do easily with this code.

$(document).ready(function(){
  $( "p.tolower" ).each(function( index ) {
   var text= $(this).text().toLowerCase();
   $(this).html(text);
  });});
p.tolower{text-transform: capitalize;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class='tolower'>HELLO I AM HERE</p>
<p class='tolower'>HELLO I AM HERE AGAIN</p>
<p class='tolower'>HELLO I AM HERE AGAIN AND AGAIN</p>
Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38