user3722851
user3722851

Reputation: 147

Javascript Send parameters from button to function

       $.getJSON('/CourtHouseManagement/LoadLawCourt/?cityId=' + id, function (result) {

                $('#JusticeCourtTable').html('');

                for (var i = 0; i < result.length; i++) {

                    var tablestring =

                        '<tr>' +
                        '<th>' + result[i].CourtID + '</th>' +



'<th><button type="button" class="btn btn-sm btn-primary" onclick="javascript:selectClaimant(' + result[i].CourtID + ',\'' + result[i].Name +');">Update</button></th>'+   //Problem HERE

'<th><button type="button" class="btn btn-sm btn-primary" onclick="javascript:selectClaimant(' + result[i].CourtID + ',\'' + result[i].Name +');">Delete</button></th>'  //Problem HERE


                    tablestring += '</tr>';
                    $("#JusticeCourtTable").append(tablestring);
                }
            });
        }
    }


    function selectClaimant(CourtID, Name) {  

        alert(CourtID + Name);


    }

I load datas to table.If **i click to "Delete" or "Update" button , ı want to send CourtID and Name for selected row.**I tried above code.When i click to "Update" or "Delete" button , i get below exception.

Uncaught SyntaxError: Unexpected number 

When i click to button "selectClaimant" function never works.Where i miss exaclty ? How can i send CourtID and Name on "Update" or "Delete" button click ?

Any help will be appreciated.

Thanks.

Upvotes: 0

Views: 58

Answers (1)

artuc
artuc

Reputation: 913

Have you tried like this:

$.getJSON('/CourtHouseManagement/LoadLawCourt/?cityId=' + id, function (result) {
        $('#JusticeCourtTable').html('');
        for(var i = 0; i < result.length; i++){
            var tablestring ='<tr><th>'+result[i].CourtID + '</th>' +'<th>'+
            '<button type="button" class="btn btn-sm btn-primary" onclick="selectClaimant(\'' + result[i].CourtID + '\',\'' + result[i].Name +');">Update</button></th>'+
            '<th><button type="button" class="btn btn-sm btn-primary" onclick="selectClaimant(\'' + result[i].CourtID + '\',\'' + result[i].Name +');">Delete</button></th>'

            tablestring += '</tr>';
            $("#JusticeCourtTable").append(tablestring);
        }
    });

Also, it's possible to find the values this way:

  $.getJSON('/CourtHouseManagement/LoadLawCourt/?cityId=' + id, function (result) {
        $('#JusticeCourtTable').html('');
        for(var i = 0; i < result.length; i++){
            var tablestring ='<tr><th>'+result[i].CourtID + '</th>' +'<th>'+
            '<button type="button" class="btn btn-sm btn-primary" data-courtid="'+result[i].CourtID+'" data-name="'+result[i].Name+'">Update</button></th>'+
            '<th><button type="button" class="btn btn-sm btn-primary" data-courtid="'+result[i].CourtID+'" data-name="'+result[i].Name+'">Delete</button></th>'

            tablestring += '</tr>';
            $("#JusticeCourtTable").append(tablestring);
        }
    });

    $(document).on('click', '#JusticeCourtTable button', function(){
        alert($(this).data('courtid')+' - '+$(this).data('name'));
    });

Upvotes: 1

Related Questions