0

My buttons are not bringing me to the next page. When I check for errors, it gives below error:

Uncaught TypeError: Cannot read property 'categoryID' of undefined" at line 53

I am unable to find any one with the same problem as me on this platform hence, the appeal for help.

There is no errors for my PHP.

$(document).ready(function () {
getcategories();

function getcategories() {
    var url = serverURL() + "/getcategories.php";

    var JSONObject = {
        "categoryID": localStorage.getItem("categoryID")
    };

    $.ajax({
        url: url,
        type: 'GET',
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (arr) {
            _getCategoryResult(arr);
        },
        error: function () {
            validationMsg();
        }
    });
}

function _getCategoryResult(arr) {
    var t
    if ($.fn.dataTable.isDataTable('#categoryresult')) {
        t = $('#categoryresult').DataTable();
    }
    else {
        t = $('#categoryresult').DataTable({
            "searching": false,
            "lengthChange": false
        });
    }
     t.clear();

    //loop for the number of results found by getcategories.php
    for (var i = 0; i < arr.length; i++)
        //add a new row
        t.row.add([
            arr[i].categoryID,
            arr[i].categoryname,
            "<a href='#' class='ui-btn' id='btn" + arr[i].categoryID + "'>Category</a>" //add a new [Category] button
        ]).draw(false);

    $("#btn" + arr[i].categoryID).bind("click", { id: arr[i].categoryID }, function (event) { //error 
        var data = event.data;
        showcategory(data.id);
    });

}
$("#categoryresult").show();

function showcategory(categoryID) {
    //alert(categoryID);
    window.location = "showlist.html?categoryID=" + categoryID;

}

});
2
  • where is line 53.guess there is a undefined in your arr,try to console.log(arr); Commented Feb 10, 2018 at 7:56
  • 3
    dump out the arr object as you enter getCategoryResult to see the whole thing. The structure may simply be different from what you expect. Commented Feb 10, 2018 at 7:57

1 Answer 1

1

maybe you want to try this

for (var i = 0; i < arr.length; i++){
    //add a new row
    t.row.add([
        arr[i].categoryID,
        arr[i].categoryname,
        "<a href='#' class='ui-btn' id='btn" + arr[i].categoryID + "'>Category</a>" //add a new [Category] button
    ]).draw(false);

$("#btn" + arr[i].categoryID).bind("click", { id: arr[i].categoryID }, function (event) { //error 
    var data = event.data;
    showcategory(data.id);
})};

You forgot to put enclosing in your for loop, thus, arr[i] = undefined

Sign up to request clarification or add additional context in comments.

2 Comments

as a good practice, please always write the enclosing before write the codes inside ;)
Will do. Thank you so much for the help! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.