user2911849 user2911849 - 1 year ago 90
Javascript Question

Capitalize first letter of each line using JavaScript

I'd like to know how I could capitalize the first letter of a row in JavaScript like in this PHP code :


$textParts = explode("\n", $text);
foreach ($textParts as $key => $value) {
$textParts[$key] = ucfirst($value);

$text = implode("\n", $textParts);

$text="namjaga da geureochi mwo narago dareugetni
ni mameul da gajyeodo naragabeorigo maneun
namjaga da geureochi mwo"


<?php echo $text; ?>

In JS, I get the text into the "Roman" DIV after a tranformation :

<div id="Roman"> </div>


<?php include("../../tools/js/romantool.js"); ?>
function affichar() {
document.getElementById('Roman').innerHTML = latin_to_roman(


Does anyone know how I should proceed to do as in the previous PHP example ? Thank you !

Answer Source

In fact I would go right in between the two suggested answers so far. I would use javascript to replace the line breaks with a tag that will add structure around text e.g.

var str = document.getElementById('Parol').innerHTML;
str = '<span>' + str.replace(/(?:\r\n|\r|\n)/g, '</span><span>') + '<span>';
document.getElementById('Roman').innerHTML = str;

And than style it in whichever way you want using CSS, your case would be

   span {
        display: block;

    span:first-letter {
        text-transform: uppercase;

Here's a fiddle

Adding structure to your text will help you manipulate it much easier

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download