0

I have a table, and the last column in the table has a checkbox, when the user clicks on a a check box, i want to pass a row value to a different function, but when i do this, the code doesn't seem to be firing, am i missing something ?

 function GetContractors(companyName) {

    var workforce = companyName;

    $.ajax({

        type: 'GET',
        url: '@Url.Action("getContractors","Permit")',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: { "workforce": workforce },
        success: function (result) {

            $("#mainData").empty();

            $.each(JSON.parse(result),
                function (i, item) {

                    var row = i + 1;

                    $("#mainData").append(

                        "<tr>" +

                        "<td id='process_"  + row + "'" + ">" + item.ContractorName + "</td>" +

                        "<td bgcolor= " + "'" + cellColour1 + "'" + " id='process_" + row + "'" + ">" + parseJsonDate(item.InductionExpiry) + "</td>" +

                        "<td bgcolor= " + "'" + cellColour + "'" + " id='process_" + row + "'" + ">" + parseJsonDate(item.InsuranceExpiry) + "</td>" +

                        "<td id='Action" + row + "'" + ">" +

                        "<checkbox id='checkbox' class='btn btn-xs btn-success " +
                        "'" + "onclick='SelectedUser(" + item.ContractorName + ")" +
                        "'" + ">" + "<i class='glyphicon glyphicon-trash" +
                        "'" + "></i>" + "</checkbox>" + "&nbsp" +

                       "</tr>");

                });
        },
        error: function (result) {

            alert(result);
        }
    });
};

  function SelectedUser(name) 
  {
    alert(name);
    }
10
  • want to pass a row value to a different function You are just passing { "workforce": workforce }, Am I missing something? Commented Nov 8, 2018 at 12:14
  • If value of item.ContractorName is say john, javascript will see the onclick handler code as SelectedUser(john), so javascript wil ltry to look for a variable named john which doesnt exists and error will be thrown. TLDR: Hardcoded string argument is missing quotes. Commented Nov 8, 2018 at 12:16
  • i want to pass "contractorName" to the function selectUser when the checkbox is clicked Commented Nov 8, 2018 at 12:16
  • try onchange instead of onclick. Plus see in your DOM what exactly is rendered. Commented Nov 8, 2018 at 12:22
  • Declare your function "window.SelectedUser = function(name){ ... };" Commented Nov 8, 2018 at 12:24

2 Answers 2

2

Try this:

"onclick='SelectedUser(\"" + item.ContractorName + "\")" +

You were passing:

onclick='SelectedUser(Bob)'

which would expect an object, but should be

onclick='SelectedUser("Bob")'

\" escapes the double-quote

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

1 Comment

Legend! All i was missing was the single quotes, as i compared this in the DOM
1

If the value is a string pass it between apostrophes, like so:

SelectedUser(" ' + item.ContractorName + ' ").

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.