0

I've created a crossword game based on this: https://github.com/trutex/jCrossword. I added a script to score the game (1 point per correct answer). Goal: when the user has answered all the hints they can, they can post their score to a SharePoint list, "Crossword Scores". Ideally, the value of the score (clueScore) should be passed to the list as a new list item on Submit. If that can't work, I'd like them to enter the score in the form field, "scoreField" and Submit. I can't get it to pass clueScore to scoreField through javaScript, and there is something in the rest of the code that disables typing in the form after I've entered any clues in the puzzle, so manually typing in the form is out too.

Codepen: https://codepen.io/mrsgorgon/pen/jOyXqpb

HTML:

<div id="scoreBox">SCORE: <span id="scoreSpan">0</span></div>
<p>When you've filled in as many clues as you can, click the SUBMIT button.</p>
  <label for="scoreField">Score:</label><br>
  <input type="text" id="scoreField" name="scoreField"><br><br>
  <input type="button" value="Submit Your Score!" onclick="createListItem()">
<br><br>

<div id="divTest"></div>

CSS: See CodePen https://codepen.io/mrsgorgon/pen/jOyXqpb

JavaScript (see CodePen for puzzle building function):


//Post score to SharePoint list
var siteUrl = 'https://www.mySite.com';
var scoreValue = $('[name="scoreField"]').val();

function createListItem() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Crossword%20Scores');
        
    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
        
    oListItem.set_item('Title', 'scoreValue');
  
        
    oListItem.update();

    clientContext.load(oListItem);
        
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
8
  • The real problem is that the code you pasted does not save anything to SharePoint? Apr 26, 2021 at 23:05
  • See the CodePen link for the rest of the code. It was too long to fit here. I can post to the list manually if i do it BEFORE starting the game. Something in the game disables posting manually afterwards.
    – mrsgorgon
    Apr 27, 2021 at 21:08
  • by "posting manually" what do you mean exactly? Apr 27, 2021 at 21:09
  • I mean typing the calculated score value into the form and then clicking Submit, rather than having Submit pass the calculated score value to the list without typing it in the form.
    – mrsgorgon
    Apr 27, 2021 at 21:13
  • Preferred method: javascript calculates score. Submit passes it to the SP List. If I can't have that: have user enter score in form, Submit passes the form value to the SP list.
    – mrsgorgon
    Apr 27, 2021 at 21:14

1 Answer 1

0

Not the best answer, but I would start with this:

function createListItem( ) {
    //Post score to SharePoint list
    var siteUrl = 'https://www.mySite.com';
    var scoreValue = $('[name="scoreField"]').val();

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Crossword Scores');
        
    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
        
    oListItem.set_item('Title', score);
  
        
    oListItem.update();

    clientContext.load(oListItem);
        
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

// call the function that 
createListItem();

I removed "%20" from the "Crossword Scores". Make sure you use the actual display name of your list, not its URL.

also, updated this line to save the actual score passed:

oListItem.set_item('Title', score);

Also, before you click submit, open the browser dev tools by clicking F12 and see if there are any errors occuring. If they do read them carefully and try to fix them.

1
  • Is there anyway to pass clueScore directly without using the form? Something about the puzzle disables form fields once the user starts answering clues.
    – mrsgorgon
    Apr 27, 2021 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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