1

I've created a button in this context: enter image description here

And I have a javascript function expecting the click to make a request to the server: enter image description here

But something weird happens when I hit the button, the page reloads it all and don't take the action expected. Thank you all in advance, im gettin crazy with this.

When I put an alert to check if the button is triggered I get an error: enter image description here

enter image description here

R.Quintal
  • 43
  • 1
  • 3
  • 13

2 Answers2

2

There is a chance that you get this behavior because of the form tag. Try to remove the form tag and try again.

Maybe this can also help you: Stop form refreshing page on submit

Stasel
  • 1,298
  • 1
  • 13
  • 26
  • Omg... I can't believe thats the problem... Its solved! Thank you. But one last question, its better to get rid off the form tag or use te "preventDefault()" function? – R.Quintal Feb 26 '18 at 12:38
  • @R.Quintal as @Marcel Ennix suggested in his comment, you can just add `type="button"` to you button and it shouldn't submit the form natively – Stasel Feb 26 '18 at 13:01
  • yes, that's what I've done and It works! Thank you all – R.Quintal Feb 26 '18 at 13:48
1

Try insert: type="button" inside the button tag. Otherwise it will be a submit button (when inside a form tag).

Marcel Ennix
  • 1,328
  • 1
  • 12
  • 16