I have some JavaScript/jQuery that I am trying to run locally for testing. It begins with $( 'form' ).submit(function(event) {
, but, never gets beyond that. (I put a console.log
just after that line. It never executes. I know the js is being executed because a console.log
before that line does execute.) A syntax checker gives no errors.
JavaScript:
$( 'form' ).submit(function(event) {
const SelectElms = $( 'select' );
var a = [];
var selectVals = Array.from(SelectElms).reduce((a,n) => {
if(n.value && n.value !== 'unsure' && n.value !== 'not attend') {
a.push( {
'index': n,
'room': n.value
}
);
} // end if
return a;
}); // end def arrow fn & reduce
event.preventDefault();
}); // end def anonymous fn & submit
In the snippet below, the form displays correctly, but, on submit, the console flashes an error message for a second, then disappears. It displays for too little time to read it. Then, the error message "The custom error module does not recognize this error" appears in the results pane.
Also, if you would, please check the rest of the js for correctness. What I want to do is create an array of objects, one object per <select>
element in the form. Each object should have the index position of and the value of the <select>
element ({'index': n, 'room': n.value}).
In his answer to this prior question, MisterJojo did something similar which I tried to run locally. Although it runs in his snippet, when I run it locally, it produces a TypeError that the querySelector('#My-Form')
returns null
. I copied and pasted his js and HTML. The form has id="My-Form"
in the <form>
tag. Also, the syntax checker returns several errors. So, I haven't been able to implement his solution or adapt it to this problem. Note: it does run in this jsfiddle.
Thanks so much for your help.
Snippet:
$( 'form' ).submit(function(event) {
const SelectElms = $( 'select' );
var a = [];
var selectVals = Array.from(SelectElms).reduce((a,n) => {
if(n.value && n.value !== 'unsure' && n.value !== 'not attend') {
a.push( {
'index': n,
'room': n.value
}
);
} // end if
return a;
}); // end def arrow fn & reduce
event.preventDefault();
}); // end def anonymous fn & submit
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="post">
<div>
<label>First Name
<input type="text" name="fname" size="25">
</label>
</div>
<div>
<label>Last Name
<input type="text" name="lname" size="25">
</label>
</div>
<div>
<label>email address
<input type="email" name="email" size="25">
</label>
</div>
<div>
<label>08:30 Keynote Speaker
<select class="select" name="select 0830">
<option value="unsure">unsure</option>
<option value="attend">attend</option>
<option value="not attend">not attend</option>
</select>
</label>
<label>09:00 Classes
<select class="select" name="select 0900">
<option value="unsure">unsure</option>
<option value="A">room A</option>
<option value="B">room B</option>
</select>
</label>
<label>10:30 Classes
<select class="select" name="select 1030">
<option value="unsure">unsure</option>
<option value="A">room A</option>
<option value="B">room B</option>
</select>
</label>
</div>
<div>
<input type="submit" value="submit form">
</div>
</form>