0

I am having an issue with the a href="#" still working when the link is clicked - I can see the console.log() before the page changes - why is it doing this?

HTML:

<a href="#" id="brands_by_category_change_name_btn" class="btn btn-primary" >Save</a>

JS:

        $('body').on("click", "#brands_by_category_change_name_btn", function () {

            var self = $(this);
            var id = $("#product_name_head").data("productid");
            var cat_id = $(".product_category_selector").data("id");

            var url = $("#manufacturers_table").data("infourl");

            var state = "0";
            if(self.is(":checked"))
            {
                state = "1";
            }
            var data_array = { 
                    id : id, 
                    cat_id : cat_id, 
                    state : state
                };

                console.log(url);

            //ajaxCall(url, data_array, null, "reload_selected_product_categories");

        });  
Jess McKenzie
  • 8,345
  • 27
  • 100
  • 170
  • Check out this similar question and the answers for best practices. http://stackoverflow.com/questions/134845/href-attribute-for-javascript-links-or-javascriptvoid – Meximize Sep 19 '13 at 20:45

3 Answers3

7

You should pass event parameter to function. And do. Event.preventDefault() it will stop link from native behavior.

Gena Moroz
  • 929
  • 5
  • 9
0

Adding return false; at the end of the function will fix the problem

andrew
  • 9,313
  • 7
  • 30
  • 61
0

Add a return false; at the end of your event-handling code. I would do it that way:

$("#brands_by_category_change_name_btn").click( 

  // your code...

  return false;

}); 
jotaen
  • 6,739
  • 1
  • 11
  • 24