1

I have a dynamic table which is being created with jquery. The problem I face is, jquery interferes my string so it's not being parsed correctly to onclick method.

for (var i = 0; i < data.result.list.length; i++) 
{
    var tr = $("<tr></tr>");                       
    tr.append("<td>" + "<a onclick='" + data.result.list[i].cfunction + "'>" + data.result.list[i].cvalue + "</a>" + "</td>");
    tr.append("<td>" + + "</td>");
    table.append(tr);
}

data.result.list[i].cfunction has a string value like "getMyMethod('My parameter')" which is basically calling a method with parameter in quotes. But after jQuery parses this to onclick function it's value changes as onclick="getMyMethod(" my="" parameter')'=""

here is the generated html:

<a onclick="getMyMethod(" my="" parameter')'="" >My Value</a>

Can anyone put me in the right direction?

5
  • 1
    Can you show the generated HTML? Commented Mar 23, 2018 at 6:59
  • Please post code regarding how you get your data. via Ajax call?? Commented Mar 23, 2018 at 7:02
  • @31piy I added it. Do you think is it clear? Commented Mar 23, 2018 at 7:02
  • can you log data.result.list[i].cfunction ? Commented Mar 23, 2018 at 7:04
  • use escape like alert("\"Hello\""); Commented Mar 23, 2018 at 7:06

5 Answers 5

1

Change this line

  "<a onclick='" + data.result.list[i].cfunction + "'>"

to this

  '<a onclick="' + data.result.list[i].cfunction + '">'
Sign up to request clarification or add additional context in comments.

Comments

1

You internal double quotes " needs to be escaped

var func = data.result.list[i].cfunction.replace( /"/g, "\\\"" );
tr.append("<td>" + "<a onclick='" + func + "'>" + data.result.list[i].cvalue + "</a>" + "</td>");

1 Comment

Hello, internal double quotes are being generated as I'm parsing it to onclick function. There's no internal double quotes in the first place.
1

you should modify code with:

tr.append(
    $("<td></<td>").append(
        $("<a></a>")
            .attr({
                "href" : "#"
            })
            .text(data.result.list[i].cvalue)
            .attr({
                "onclick" : data.result.list[i].cfunction
            })
    )
);

1 Comment

although I've marked another answer as accepted, I find this approach more readable and better, thanks a lot for providing this.
1

You can modify your cfunction items in the data like this (encode):

cfunction: "getMyMethod(\"My parameter\")",

This worked in the console (make sure the table ID exists):

var data = {
    result: {
        list: [
            {
                cfunction: "getMyMethod(\"My parameter 1\")",
                cvalue: "1"
            },
            {
                cfunction: "getMyMethod(\"My parameter 2\")",
                cvalue: "2"
            }
        ]
    }
}

var table = $("#testTable");

for (var i = 0; i < data.result.list.length; i++) {
    var tr = $("<tr></tr>");                       
    tr.append("<td>" + "<a onclick='" + data.result.list[i].cfunction + "'>" + data.result.list[i].cvalue + "</a>" + "</td>");
    tr.append("<td>" + + "</td>");
    table.append(tr);
}

var getMyMethod = function (funcParam) {
    console.log(funcParam);
};

Comments

1

Can you try this. And be sure your data.result.list[i].cfunction value is "getMyMethod('My parameter')"

for (var i = 0; i < data.result.list.length; i++) 
{
    var tr = $("<tr></tr>");                       
    tr.append("<td>" + "<a onclick=\" " + data.result.list[i].cfunction + 
"\" >" + data.result.list[i].cvalue + "</a>" + "</td>");
    tr.append("<td>" + + "</td>");
    table.append(tr);
}

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.