163

I have a button (<input type="submit">). When it is clicked the page reloads. Since I have some jQuery hide() functions that are called on page load, this causes these elements to be hidden again. How do I make the button do nothing, so I can still add some action that occurs when the button is clicked but not reload the page.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ankur
  • 50,282
  • 110
  • 242
  • 312

11 Answers11

327

There is no need to use JS or jQuery. to stop the page to reload, just specify the button type as 'button'.

If you don't specify the button type, the browser will automatically set it to 'reset' or 'submit' which causes the page to reload.

<button type='button'>submit</button> 
Michael M.
  • 10,486
  • 9
  • 18
  • 34
Jafar Rasooli
  • 3,437
  • 2
  • 14
  • 13
87

Use either the <button> element or use an <input type="button"/>.

davidsbro
  • 2,761
  • 4
  • 23
  • 33
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 117
    The – AdamC Jun 27 '12 at 23:32
  • 2
    you can still use form onsubmit event and return false if you still don't want to return – neu-rah Jun 27 '12 at 23:37
  • @AdamC This is not true. No browser refreshes when you click on a – pbeardshear Jul 06 '12 at 01:03
  • 4
    @Joe actually unfortunatelly it does :/ I need my webapp to work for IE8 and that reload is kinda annoying... – Ms. Nobody Sep 03 '13 at 06:39
  • 27
    @pbeardshear It does if it is placed within a form. when ` – Kevinvhengst May 14 '14 at 23:36
  • 2
    I looked this up because my ` – BobRodes Jun 27 '16 at 00:10
  • 15
    add `type="button"` to the ` – Johnny Metz Mar 13 '17 at 20:13
  • `` prevents submission if it's inside a form (FF 59, Chrome 65). ([button](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button) at MDN) – handle Apr 17 '18 at 14:06
43

In HTML:

<form onsubmit="return false">
</form>

in order to avoid refresh at all "buttons", even with onclick assigned.

user2868288
  • 589
  • 5
  • 9
  • 3
    Should be correct answer. Using a button breaks the submit and require function of the form – jona Jun 16 '22 at 11:12
15

You could add a click handler on the button with jQuery and do return false.

$("input[type='submit']").click(function() { return false; });

or

$("form").submit(function() { return false; });
Chris Gutierrez
  • 4,750
  • 19
  • 18
  • 6
    This doesnt work. Returning false in chrome, at least in latest versions, still causes refresh. – user3690202 Jun 17 '14 at 19:57
  • This is the only answer which actually worked for me. I just aded return false in my function and the refresh stopped in both safari and chrome. – VessoVit Dec 08 '16 at 01:15
14

In HTML:

<input type="submit" onclick="return false">

With jQuery, some similar variant, already mentioned.

pestilence669
  • 5,698
  • 1
  • 23
  • 35
13

You can use a form that includes a submit button. Then use jQuery to prevent the default behavior of a form:

$(document).ready(function($) {
  $(document).on('submit', '#submit-form', function(event) {
    event.preventDefault();
  
    alert('page did not reload');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form id='submit-form'>
  <button type='submit'>submit</button>
</form>
ProgrammingWithRandy
  • 725
  • 3
  • 14
  • 31
6

You could also use JavaScript for that:

  let input = document.querySelector("input");
  input.addEventListener("submit", (event) => {
    event.preventDefault();
  })
mac125
  • 93
  • 1
  • 7
5

As stated in one of the comments (burried) above, this can be fixed by not placing the button tag inside the form tag. When the button is outside the form, the page does not refresh itself.

Pablo Gonzalez
  • 1,710
  • 3
  • 21
  • 36
2

I can't comment yet, so I'm posting this as an answer. Best way to avoid reload is how @user2868288 said: using the onsubmit on the form tag.

From all the other possibilities mentioned here, it's the only way which allows the new HTML5 browser data input validation to be triggered (<button> won't do it nor the jQuery/JS handlers) and allows your jQuery/AJAX dynamic info to be appended on the page. For example:

<form id="frmData" onsubmit="return false">
 <input type="email" id="txtEmail" name="input_email" required="" placeholder="Enter a valid e-mail" spellcheck="false"/>
 <input type="tel" id="txtTel" name="input_tel" required="" placeholder="Enter your telephone number" spellcheck="false"/>
 <input type="submit" id="btnSubmit" value="Send Info"/>
</form>
<script type="text/javascript">
  $(document).ready(function(){
    $('#btnSubmit').click(function() {
        var tel = $("#txtTel").val();
        var email = $("#txtEmail").val();
        $.post("scripts/contact.php", {
            tel1: tel,
            email1: email
        })
        .done(function(data) {
            $('#lblEstatus').append(data); // Appends status
            if (data == "Received") {
                $("#btnSubmit").attr('disabled', 'disabled'); // Disable doubleclickers.
            }
        })
        .fail(function(xhr, textStatus, errorThrown) { 
            $('#lblEstatus').append("Error. Try later."); 
        });
     });
   }); 
</script>
El Gucs
  • 897
  • 9
  • 18
0

Use event.preventDefault() in the first line of your event function.

Buttons must be of the type button and contain type="submit" in the button html.

shafee
  • 15,566
  • 3
  • 19
  • 47
Vegozzy
  • 91
  • 1
  • 3
0

Add a to the botton and input data-toggle="modal" like this

<a data-toggle="modal"><button type="submit">Edit Profile</button></a> 
Arif Maksum
  • 71
  • 1
  • 2