dimanche 12 avril 2015

Submit form via JavaScript to the correct Post action URL in the background

I have the following form:



<form action='/post' name='submitform' id="submitform" method='post' class='pure-form'>[...]
<textarea columns="40" rows="4" name='entry[body]' id="statement"></textarea>
<input type='submit' id="submitbutton" name="btnSubmit" value="save" class="pure-button pure-button-primary">
</form>


When the user clicks on the submit button I want the form to be processed first by a script that will check the contents of #statement and then do a Post request for the form in the background. I prefer to do it in JavaScript (not jQuery).


I tried using



document.querySelector('#submitform').addEventListener('submit', function(e) {
e.preventDefault();
console.log('attempt');
// so smth to submit the form in background - what?
});


but it doesn't seem to "catch" the form and it still submits...


What am I doing wrong?


UPDATE - had a typo in html


Aucun commentaire:

Enregistrer un commentaire