Search code examples
jqueryajaxthisattr

How to post ID of span using ajax


I need post ID of span using ajax, but it doesnt work :D alert($(this).attr("id")) work so why doesnt work ajax?

$(".nav-item").click(function() {
    $.ajax({
        method: "POST",
        url: "load.php",
        data: {
            page: $(this).attr("id");
        }
    }).done(function(result) {
        $("#main").html(result);
    });
});

Solution

  • If thats exact copy of your code then remove that semicolon after page:

    data: {
        page: $(this).attr("id"); <---- remove this semicolon
     }
    

    ...otherwise, your code is correct, here is an example (check console > network requests > payload sent by request): https://jsfiddle.net/co6yupzg/