Home > Blockchain >  Is there another way to pass an object to a function?
Is there another way to pass an object to a function?

Time:04-13

I have a datatable which is requesting data from an API. I am getting an object called full which has all the variables from the database. I can access integers without a problem and pass them from a function upon a button click but a string is bringing an error saying Uncaught SyntaxError: missing ) after argument

<script>
$(document).ready(function () {
$("#datatable").DataTable({
        "processing": true,
        "serverSide": true,
        "filter": true,
        "ajax": {
            "url": '@TempData["api"]api/Versioning/Review',
            headers: {
                'Authorization': 'Bearer @HttpContextAccessor.HttpContext.Session.GetString("token")'
            },
            "type": "GET",
        },
        "columnDefs": [{
            "targets": [0],
            "visible": false,
            "searchable": false
        }],
        "columns": [
            { "data": "name", "name": "Name", "autoWidth": true },
            { "data": "name", "name": "Name", "autoWidth": true },
            {
                data: "created_at",
                "render": function (value) {
                    if (value === null) return "";
                    return moment(value).format('DD/MM/YYYY');
                }
            },
            {
                "render": function (data, type, full, meta) {
                    return '<button onclick="changes(' full.changes ')" ><i ></i> Changes</button>';
                }
            },
            {
                "render": function (data, type, full, meta) {
                    return "<button id='remove' onclick='approve(" full ")' class='btn btn-success'><i class='fas fa-check-circle'></i> Accept Version</button>";
                }
            },
            {
                "render": function (data, type, full, meta) {
                    return "<button id='deny' onclick='deny("   full.id   ")' class='btn btn-danger'><i class='fas fa-minus-circle'></i> "   full.changes   "</button>";
                }
            },

        ]
    });
});

<script/>

Above i am requesting the data and i cant received the full.changes on my function and i cant even log it. But when am passing in an ID its working and i can log it. I also tried passing in the full object full and then accessing it in my function like full.changes but still its not working. Below is my function

    <script>
    function changes(changes) {
        console.log(changes)

}
</script>

Basically what i want is to log the variable called changes which is in the full object but so far no success. Anyone know how i can pass it?

CodePudding user response:

You should produce quotes around a string literal in your HTML. They are missing.

So replace this:

return '<button onclick="changes(' full.changes ')" ><i ></i> Changes</button>';

With

return '<button onclick="changes(\'' full.changes '\')" ><i ></i> Changes</button>';

Do similar things (with the appropriate quote and escaping) in the other cases.

  • Related