1

I hava a SP custom list, I want to get only first letter of all list items but the proble is that I get only the first letter of first item.

my code:

function onS() {
    var lstString = "";
    var Enum = listItems.getEnumerator();
    while (Enum.moveNext())
    {
        var currentItem = Enum.get_current();
        lstString += currentItem.get_item("Title") + "<br/>";

    }
    $("#divCustomer").html(lstString.substring(0,1));//here I am using substring to get only the first letter and it works but it return only the first letter of first item (I have 10 items in custom list).
}
  • 1
    You are doing the substring after your while loop over all items. So you concatenate all Titles and then get the first letter of that (very long) string – Danny '365CSI' Engelman Oct 28 '15 at 10:27
1

Change code to this:

function onS() {
    var lstString = "";
    var Enum = listItems.getEnumerator();
    while (Enum.moveNext())
    {
        var currentItem = Enum.get_current();
        lstString += currentItem.get_item("Title").substring(0,1) + "<br/>";

    }
    $("#divCustomer").html(lstString);
}
| improve this answer | |
1
    var letters = [];//it is an array
    var Enum = listItems.getEnumerator();
    while (Enum.moveNext()) {
        var currentItem = Enum.get_current();
        var letter = currentItem.get_item("Title").substring(0,1);
        letters.push( letter );//push one letter on the array
    }
    var lettersHTML = letters.join('<br/>');//create String with HTML between all letters
    $("#divCustomer").html( lettersHTML );

This separates your HTML from the processing and makes for better maintainable code.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.