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.....

2 comments:

  1. Sega Genesis - Crown Retro
    The authentic air jordan 5 shoes Online Sega Get air jordan 15 retro Genesis was originally melbet released as the TurboGrafx-16 cheap jordan 1 retro home video game console, and the later replica retro jordans successor to the SNES, the Mega Drive/Genesis.

    ReplyDelete