This.form.submit () does not work after clicking a div element in the form

I tried to check the presentation form with mouse clicks, but the form does not seem to be sent with javascript using vanilla.

I use this simple markup and code:

<form name="form" id="price" action="" method="post"> <div class="category" name="price" value="50 dollars" onClick="this.form.submit();" >price</div> </form> <?php echo $_POST['price']; ?> 

I can submit the form using jQuery, but I donโ€™t understand why this.form.submit() does not work with javascript in vanilla? I use Chrome to verify this.

+6
source share
5 answers

A div not a form element. There is no this.form for it.

You can still do document.forms.form.submit() ( .form , since you have name="form" )

+18
source

Your code may work if you tried something like this:

 onClick="document.forms["price"].submit();" 

this in your case actually refers to the div tag, and not to the document object that contains the link to the form.

+4
source

Use the following code if your form name is a "filter form"

 onclick="return document.forms.filter-form.submit();" 
+1
source

A div not a form element (thus, the .form property) and does not matter. I think you wanted <input> instead of <div> .

0
source

This is an older question, but this should work

 onclick="this.parentNode.form.submit()" 
0
source

Source: https://habr.com/ru/post/903081/


All Articles