Create and add data to a file in SharePoint Online using JavaScript

$(document).ready(function () {

    clientContext = SP.ClientContext.get_current();

    $(“#btnPublish”).click(function () {
        getSPItems();
    });

    function getSPItems() {

        var spList = clientContext.get_web().get_lists().getByTitle(‘Team_Members’);

        var camlQuery = new SP.CamlQuery();

        listItems = spList.getItems(camlQuery);

        clientContext.load(listItems);

        clientContext.executeQueryAsync(saveHtmlFile, onFailure);
    }

    function saveHtmlFile() {

        // var fileContent = uiContext.getJSContent();
        var fileContent = “”;
        Enum = listItems.getEnumerator();
        while (Enum.moveNext()) {
            GetTitle = Enum.current.get_fieldValues();
            fileContent += ” <div style=’position:relative;padding-bottom:10px’><img src='” + GetTitle.Image.$1_1 + “‘ alt=’employee Images’ style=’position:absolute’ height=’87’ width=’87’> <ul style=’padding-left:125px;list-style-image:url(../Images/tinyArrow.gif)’><li style=’padding-bottom:5px’><span style=’font-weight:bold’>” + GetTitle.Title + “</span></li><li style=’padding-bottom:5px’><span style=’font-weight:bold’>” + GetTitle.Position + “</span></li><li style=’padding-bottom:5px’><span style=’font-weight:bold’>” + GetTitle.Department1 + “</span></li><li class=’lastInRow’ style=’padding-bottom:5px’>” + GetTitle.Bio + “</li></ul></div>”;

        }

        var jsContent = “(function () {“
                          + “$(‘#divContent’).html(“” + fileContent + “”);”
                          + “})();”;

        var filesLibrary = clientContext.get_web().get_lists().getByTitle(“JSCompile”);
        var fileCreateInfo = new SP.FileCreationInformation();

        fileCreateInfo.set_url(“publish.js”);
        fileCreateInfo.set_overwrite(true);
        fileCreateInfo.set_content(new SP.Base64EncodedByteArray());

        for (var i = 0, fileLength = jsContent.length ; i < fileLength; ++i) {
            fileCreateInfo.get_content().append(jsContent.charCodeAt(i));
        }

        // Upload the file to the root folder of the document library
        this.newFile = filesLibrary.get_rootFolder().get_files().add(fileCreateInfo);
        clientContext.load(this.newFile);
        clientContext.executeQueryAsync(updateContentVersionNumber, onFailure);
    }

    function onFailure(sender, args) {
        alert(“Publish Failed: ” + args.get_message() + “nIf this error continues please contact: thesharepointhelper@gmail.com”);
    }

    function updateContentVersionNumber() {
        alert(“Publish Successful”);
    }

});