Skip to content Skip to sidebar Skip to footer

How To Capitalize First Letter Of Every Single Word In A Text Area?

i have a multiline textfield('Adressfeld'), and i want to Uppercase every first letter and LowerCase the rest of every single word in this text area. Here is my try: function ca

Solution 1:

I have included an example below try like that, it will solve your problem

Html

<input type="button" value="clk" onclick="z();"/>
<textarea rows="4" id="text" cols="50">

JS

function z()
{

var z=document.getElementById("text").value;
var x=z.replace(/\b./g, function(m){ return m.toUpperCase(); });
alert(x);
}

DEMO

I you want to Convert Every first letter of each word to upper and all other letters are lower then first convert the entire string to lowercase.Then do the same things as above.

DEMO2


Solution 2:

Meinst du sowas?

var textToArray = document.getElementById('myTextarea').value.split('\n');
/* Hier eine Schleife zur Bearbeitung von textToArray */
var arrayToString = textToArray.join(' ');

Solution 3:

The split operation fails -- the result of the first split cannot be split again on another character.

I see no reason to first replace returns by a single type \n and then try to split on either \n or a space. It's way easier to just replace the returns by a single space and only then split:

var strArr = String(Eingabe.value).replace(/[\r\n]+/g," ").split(" ");

With that, the rest seems to work.


Here is another approach, which may or may not work as well (it depends on whether Javascript's interpretation of "word boundary" \b agrees with your own):

function capitalize(Eingabe){
// Eingabe = this.getField("Adressfeld");
    var strArr = String(Eingabe.value).replace(/[\r\n ]+/g,' ');
    strArr = strArr.replace (/\b[a-z]/g, function(found) { return found.toUpperCase(); });
    return strArr;
}

Solution 4:

A few things:

• The argument of the function is Eingabe. In this case, this is a variable containing a value, and it does not make sense at all to redefine in the first line of the function. The way the function looks like, you won't need an argument, and therefore, your function definition looks like this:

function capitalize() {

• That done, define Eingabe properly as var Eingabe .

• With the array, you essentially want to create a two-dimensional array. First create the array of lines and then a loop through the lines, as it has been suggested in the answer by @Entimon

• You end with

this.getField("Adressfeld").value = capitalize() ;

And that should do it.


Solution 5:

thanks for your help! The correct answer is based on Arunprasanth KV's jsfiddle.net/svh1jd99

function capitalize()

{
var capitalize = this.getField("Adressfeld").value;
var done = capitalize.replace(/\b./g, function(m){ return m.toUpperCase();});
return done;
};

this.getField("Adressfeld").value = capitalize();

Thanks again for your help.


Post a Comment for "How To Capitalize First Letter Of Every Single Word In A Text Area?"