Thursday 4 July 2013

Beware the eyes of March (and missing event.PreventDefault)

Just a quick tip, I recently had a click event on an anchor tag and a get that loaded in some content when clicked like so:

$('#myid').click(function() {
     $.get('mvcurlhere', function (data) {
          //load my div here
      })
});

if you find yourself scratching your head when your MVC call using ajax and $.get seems not to be returning its because you didn't stop the original anchor from calling back, once more if your anchor tag has a # as the href which we all do from time to time, you will get the same page back with a # appended to the url and all sorts of great stuff starts happening my friends (especially with Chrome)

Solution ? remember the JQuery event.preventDefault call :

$('#myid').click(function(event) {

     event.preventDefault();    

     $.get('mvcurlhere', function (data) {
          //load my div here
      })
});

Happy days.....