0

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!

2 Answers 2

2

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.

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

Comments

1

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!

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.