I have a form I am trying to submit with HTML and JavaScript. I have attached an event listener to my checkboxes (.checkbox
), that waits for a click, and upon a click, launches a function that attempts to submit the form.
I have succeeded with jQuery's .parent()
usage, but am now attempting to switch that to vanilla JS.
I have tried this.parentNode.submit();
, however it gives back the error message.
this.parentNode.submit is not a function at HTMLInputElement.formSubmit
Is there a possible way I can submit my form by replacing the jQuery $(this).parent().submit()
to a vanilla JS equivalent?
HTML:
<form id="theform" action="/phones/search_results" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="✓">
<label>
<input type="checkbox" name="brand_name" id="brand_name" value="Apple" class="Apple brand checkbox" style="height: 30px;">
Apple
</label>
$(function() {
var checkbox = document.querySelectorAll(".checkbox");
for (var i = 0; i < checkbox.length; i++) {
checkbox[i].addEventListener("click", formSubmit)
}
});
function formSubmit(){
if(this.checked){
$(this).parent().submit();
console.log("Form was submitted" + this.parentNode)
}
}