0

I have an HTML button in my SharePoint hosted app like this:

<input type="button" id="btnSubmit" value="Test"/>

and I tried

<button id="btnSubmit">Test </button>

But it is always refreshing until I remove all the code from SharePoint and leave it as regular HTML page.

1 Answer 1

1

Your buttons are in a SharePoint FORM, so when you add a button the click event is bubbling up to the FORM, and the FORM is submitted.

You have to add JavaScript code to stop that from happing with

event.stopPropagation();

or

event.preventDefault();

See: https://codeplanet.io/preventdefault-vs-stoppropagation-vs-stopimmediatepropagation/

4
  • thanks Danny, but doesn't this happen only with <button> not with <input> ?
    – Shang Hui
    Commented Feb 23, 2015 at 13:17
  • Button is default of type submit, use type="button" as in your first example and you will be fine, not need for the JS
    – eirikb
    Commented Feb 23, 2015 at 21:24
  • thanks eirkib, I noticed that when I remove the sharepoint content place holders and just leave the <web part pages> the refresh goes away, but when I do that on regular page of sp, it's back. any idea?
    – Shang Hui
    Commented Feb 23, 2015 at 22:35
  • Check the resulting markup, if the <button> resides inside a <form> it will trigger form submit on click
    – eirikb
    Commented Feb 23, 2015 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.