Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected web page refresh in Chrome browser

I've been facing this weird behavior for a while now and can't find any workaround.

There is a button with certain methods called on click. In Firefox works well. In Chrome it just refreshes the whole page.

$("#modoComparativa").click(function(){
   if($(this).hasClass("active")){
                $('#histFromDate').attr("placeholder","Date 1");
                $('#histToDate').attr("disabled","disabled").attr("placeholder","Date 2");
                startDatepickerComp();
            }
            else{
                $('#histFromDate').attr("placeholder","Initial date");
                $('#histToDate').attr("disabled","disabled").attr("placeholder","Final date");
                startDatepicker();
            }
            $('#clearDates').attr("disabled","disabled");
            // This function calls another function causing the odd behavior in Chrome
            requestGraph(idDetail, idArea, "", "");
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

If I comment out everything but the selector and the call to the click method, the behavior is the same. It refreshes everything.

I can't figure out how to debug this as each time I press the button, the whole page refreshes and no log/errors remains in the browser debugger.

Any ideas would be appreciated.

Edit - Addition of the selector as requested:

<div class="form-group"><button class="tooltip3 btn btn-default glyphicon glyphicon-random" id="modoComparativa" data-toggle="tooltip" title="ACTIVAR COMPARATIVAS" data-placement="bottom"></button>
                            </div>
like image 980
lenord Avatar asked Dec 20 '16 18:12

lenord


1 Answers

Add a return false to your click callback to prevent actions due to your html syntax (like form submission or clicking on a anchor tag).

$("#modoComparativa").click(function(event){
    event.preventDefault();

    // your existing code
}

Edit: Since you added your html...

If your button is within a form, you can also add the type="button" attribute to prevent it from submitting your form.

<button type="button" class="tooltip3 btn btn-default glyphicon glyphicon-random" id="modoComparativa" data-toggle="tooltip" title="ACTIVAR COMPARATIVAS" data-placement="bottom"></button>

Thank you to lonesomeday for suggesting http://api.jquery.com/event.preventDefault/

like image 78
Steve Avatar answered Sep 17 '22 23:09

Steve