Html and javascript code Concatenation problem

I am trying to append HTML syntax with javascript and onClick trying to pass a function with 2 parameters. But while passing the parameters I am doing something wrong in concatenation. Please help.Here is the append code.

addPanelHtml += "<li><a href='#' id='btnhistory" + i+ "' onclick='histDrpDwnEdt("+chkval+"','" + i+ ")'>History Parameters</a></li>";

while running the program I am getting error:

Uncaught SyntaxError: Unexpected end of input

Answer

I would highly recommend to use template strings in this use case

Here the solution with template strings

addPanelHtml += `<li><a href='#' id='btnhistory${i}' onclick='histDrpDwnEdt(${chkval}, ${i})'>History Parameters</a></li>`;

Then you can get rid of all these + " " + concatenations and just use ${} to enclose your variables.

This improves the readabilty of your code by far.

Here you can read more about template strings

Leave a Reply

Your email address will not be published. Required fields are marked *