Dzshean
Dzshean

Reputation: 314

Cannot Parse String via JavaScript function

I'm dynamically generate tables row (buttons) using JS- Ajax.when i parse a numeric value removeProduct function return the alert. but i cant get alert if i parse a String. can anyone help me to solve this problem

problem is in this line : onclick='removeProduct( " + prcode + " )'

how to parse a String via function? (as a JavaScript String)

var single = alldata[i].split("##");
            var rows = "";

            var prcode = single[1];

            rows += "<td><a class='btn' onclick='removeProduct( " + prcode + " )' href='#'><i class='fa fa-trash-o'></i></a></td></tr>";
            $(rows).appendTo("#tblproductslist tbody");

Function :

    function removeProduct(str) {
    alert(str);
}

Thanks in advance!

Upvotes: 0

Views: 76

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

Because you are trying to pass a string literal, so try to enclose the value in ""

onclick='removeProduct(\"" + prcode + "\")'

Since you are working with jquery, I would recommend you use event delegation to handle event and the data-api to store the data.

Upvotes: 2

OscarGarcia
OscarGarcia

Reputation: 2114

You need this:

rows += "<td><a class='btn' onclick='removeProduct( \"" + prcode + "\" )' href='#'><i class='fa fa-trash-o'></i></a></td></tr>";

If "prcode" is a string you must to quote it or it will be treated as (undefined) variable and will trigger an error.

Good luck!

Upvotes: 1

Related Questions