0

I have a bit of javascript and jQuery where I am using jQuery .each to loop through a set of text area boxes as part of a light spellcheck routine. I am also using jQuery addClass to change the border of the text areas if a spelling criteria is not reached. The spell checking and add class portion of the code work fine. Where I am stuck is in returning an error indicator that will either flag to remain on the page because of errors or carry on to the next page.

Code is below:

function executeFormCheckSubmit() {
  function getSpellCount(essayContent) {
    return new Promise((resolve, reject) => {
      jQuery(function(\$) {
        var values = {
          'str': essayContent
        };\
        $.ajax({
          type: "POST",
          url: "/path/to/scripts/ajax/spellcheckEssay.php",
          data: values,
          success: resolve,
          error: reject,
        })
      });
    })
  }

  function checkTextArea() {
    let elementID = "";
    let errorFlag = false;
    let textAreaCheck = \$("textarea").each(function(elementID, errorFlag) {
      let textbox = this.value;
      let percentage = getSpellCount(textbox);
      elementID = this.id;
      percentage.then(function(result) {
        let grade = result.percentage;
        if (grade < 80) {
          errorFlag = true;\
          $("#" + elementID).addClass("error");
        }
      }).catch(function(error) {
        console.log('Failed', error);
      });
    });
    return errorFlag;
  }
  let isError = checkTextArea();
  if (isError === true) {
    //alert("The text does not meet our minimum requirements.");
    console.log("isError is true.");
    return false;
  } else {
    console.log("isError is false.");
    //document.course_finger.submit();
  }
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Please add your HTML as well. Its ideal to add `reproducible` code. – Always Helping Jun 29 '20 at 06:09
  • I wanted to close as a dupe, but it seems you just need help with returning a value from a promise. I think you return too soon – mplungjan Jun 29 '20 at 06:14
  • @AlwaysHelping HTML will not give much help. It is an Ajax promise issue – mplungjan Jun 29 '20 at 06:15
  • @mplungjan yes i agree to that its a promise issue. But i thought it will good to see the whole code to add a proper solution. – Always Helping Jun 29 '20 at 06:17
  • I would suggest you look into using `async await` function on that `getSpellCount` `promise` Read more about it here: https://javascript.info/async-await – Always Helping Jun 29 '20 at 06:19
  • Thank you all for your responses. Got me looking more at the ajax side. I believe that this answer may be along the lines of what I am attempting to do: https://stackoverflow.com/questions/18424712/how-to-loop-through-ajax-requests-inside-a-jquery-when-then-statment I do have a dumb down version of the web page if that will be helpful (the actual page is in legacy code). Am looping 5 text areas and spell checking them in each iteration. – Eric Peers Jun 30 '20 at 07:08

0 Answers0