0

I am stuck at calling onclick function.. here is the code..

    $(function() {
        $.each(json, function(i, item) {
            var row = '<tr>' +
                '<td>' + item.id + '</td>' +
                '<td class="project-title"><a onclick="loadData(' + item.id + ', ' + item.name + ', ' + item.description + ')">' + item.name +
                '</a></td>' +
                '<td>' + item.description +
                '</td>' +
                '</tr>';
            $('#Table tbody').append(row);
        });
    });

The issue here is that while executing the loadData function second and third arguments are not taken as string instead it takes as variable.

loadData(1, Sam, Subscriber)

Error: ReferenceError: Sam is not defined

I need both to be passed as string (i.e. value of the name: Sam and description: Subscriber) not as a variable.

Ex.

loadData(1, "Sam", "Subscriber")

Thank you!

1
  • So put the strings in escaped quotes. Commented Apr 15, 2018 at 19:35

2 Answers 2

2

Just add escaped apostrophes:

'<a onclick="loadData(' + item.id + ', \'' + item.name + '\', \'' + item.description + '\')">' + item.name + '</a>'

As result you will get:

<a onclick="loadData(1, 'Sam', 'Subscriber')">Sam</a>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks buddy... this is what exactly I wanted
0

You are answering yourself,

$(function() {
    $.each(json, function(i, item) {
        var row = '<tr>' +
            '<td>' + item.id + '</td>' +
            '<td class="project-title"><a onclick="loadData(' + item.id + ', \"' + item.name + '\", \"' + item.description + '\")">' + item.name +
            '</a></td>' +
            '<td>' + item.description +
            '</td>' +
            '</tr>';
        $('#Table tbody').append(row);
    });
});

Comments

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.