Skip to content Skip to sidebar Skip to footer

Onclick Didn't Work Inside Form?

Usually I see people use form to create input :

Solution 1:

its make my submit button didnt work

No, your submit button is working fine. Therein lies the problem you're seeing, in fact. Your JavaScript code is executing, but the time between showing any output and reloading the page because you've submitted a form is nearly instantaneous.

So your JavaScript works. And your form works. Everything works.

but as soon as i remove the form tag its work

Not quite. When you remove the form tag the form submission no longer works. All you've done is prevent the form from submitting, because you no longer have a form.

why cant i wrap all my input inside form?

You can. But it really depends on what you're trying to do. If you have a form and you want to submit it, use a form element. If you don't want to submit anything as a form, you can leave it out. That's its only purpose.

You can have inputs on the page anywhere you like, they don't need to be in forms. If you just want to interact with them via JavaScript then you can do that without a form element.

Solution 2:

Forms are made to make HTTP methods (such as POST or PUT). So, if you need that input's information take part in a http method, you NEED TO use form. If your Submit button is made just for internal stuff, don't use form.

A little more information: enter link description here

Solution 3:

Form will send the data to the file specified in action and this event is triggered by <input type="submit"/> inside a form.

You can use a <button> tag inside form to achieve what you want.

Solution 4:

You need to set the action for your form, it is empty. Or you can use jQuery

Post a Comment for "Onclick Didn't Work Inside Form?"