Question

This is the code I am working with: http://jsfiddle.net/qKyNL/12/

$('a').click(function(){
    event.preventDefault();
    var number = $(this).attr('href');
    alert(number);
    // AJAX
    $.ajax({
        url: "/ajax_json_echo/",
        type: "GET",
        dataType: "json",
        timeout: 5000,
        beforeSend: function () {
            // Fadeout the existing content
            $('#content').fadeTo(500, 0.5);
        },
        success: function (data, textStatus) {
            // TO DO: Load in new content
            // Scroll to top
            $('html, body').animate({
                scrollTop: '0px'
            }, 300);
            // TO DO: Change URL
            // TO DO: Set number as active class
        },
        error: function (x, t, m) {
            if (t === "timeout") {
                alert("Request timeout");
            } else {
                alert('Request error');
            }
        },
        complete: function () {
            // Fade in content
            $('#content').fadeTo(500, 1);
        }
    });    
});

I am trying to create a degradable pagination, using Jquery but the problem is that the "e prevent default" doesn't seem to trigger and instead it still follows the link. Could anyone please show me how I can have this link degradable so if Jquery is disabled it still works.

Was it helpful?

Solution

You're not passing in the event object. Try this:

$('a').click(function(event){ // <-- notice 'event' passed to handler
event.preventDefault();

OTHER TIPS

Should be

$('a').click(function(event){

on first line. Note that event is passed as an argument to anonymous function.

No event passing in callback.

Try

        $('a').click(function(event){
------------------------------^

            event.preventDefault();
    }

As the other answers have said you need to pass the event variable to the callback

$('a').click(function(event) {
  event.preventDefault();
});

You also need to wrap all of your code in to the ready function jQuery so that the event listeners are assigned only when jQuery is ready to handle them.

$(document).ready(function() {
    $('a').click(function(event) {
      event.preventDefault();

      // the rest of your code
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top