4

I have a mturk survey and would like to validate the results when clicking the submit button. I found out there is one way discussed here. However, that does not seem to work. Does anyone have any idea?

My code (JS part):

<script type="text/javascript">
window.onload = function() {document.getElementById('submitButton').setAttribute('onclick', 'return validateForm()'); }

function validateForm() {
    alert("test");
    return false;
}

There is no alert and the submission succeeded.

Mr.cysl
  • 1,494
  • 6
  • 23
  • 37
  • Solution: please see the answer below. Keep in mind that this should be put at the very last part of the layout HTML code. – Mr.cysl Jan 30 '19 at 21:23

1 Answers1

4

With the new Crowd HTML Elements, you can hook into the submit event and do your pre-validation like this:

document.querySelector('crowd-form').onsubmit = function(e ) {
    if (!validateForm()) {
        e.preventDefault();
    }
}

Thank you,

Amazon Mechanical Turk