Javascript to capitalize first letter

Nov 04, 2013


Does anyone have the Javascript to capitalize the first letter of username pulled from LMS?

I have the basic code that pulls the username, but it leaves all letters as lowercase.


8 Replies
Paul Kornman

Here's some Javascript for you to try:

var str = "user546";
var capFirst = str.substring(0,1).toUpperCase() + str.substring(1,str.length);

Walking through the code:

"str" holds your original string

.substring(0,1) gives you the first character of the string

.toUppercase() converts the given string to uppercase

.length gives you the length of the string

.substring(1,str.length) gives you everything after the first character

so the second statement takes the first character of str and converts it to UpperCase and then concatenates (+) that with the rest of the string (after the first character).

Using this code, capFirst will hold the string "User546"

Good luck,

Paul K.

Chris Freebairn

Thanks Paul, much appreciated.

I had actually just been tinkering and came up with this

var player = GetPlayer();
var myName = lmsAPI.GetStudentName();
var array = myName.split(',');
var str = array[1];
var newName = str.slice(0,1).toUpperCase() + str.slice(1);
player.SetVar("newName", newName);

Slightly different approach, but simialr outcome, so now others can choose from both!

Result all round

Thanks again

Alex Rossi


Learners first and last name are all caps in our LMS. I've been trying to come up with a code to correct students first and last names to title case. Here is the code that we are currently using:

var lmsAPI = parent

var rawName = lmsAPI.GetStudentName();

var nameArray = rawName.split(",")

var niceName = nameArray[1] +" "+nameArray[0];

var p = GetPlayer();


Does anyone knows the code to correct the full name to  tile case? 

Any help would be appreciated!

Paul Kornman

Try adding this function:

function initCap(strIn){
    var allChars = strIn.split(""); // convert string to array of characters
    var strOut = allChars[0].toUpperCase(); // initial character (ndex=0), force upper case
    for(var i=1; i
        strOut += allChars[i].toLowerCase(); // concatenate the lower case version or remaining
    return strOut;

and replace your line like this:

var niceName = initCap(nameArray[1]) + " " + initCap(nameArray[0]);

Good luck,

Paul K.

This discussion is closed. You can start a new discussion or contact Articulate Support.