1

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

4
  • Try, addPanelHtml += "<li><a href='#' id='btnhistory" + i + "'" + " onclick='histDrpDwnEdt("+chkval+"','" + i+ ")'>History Parameters</a></li>"; Commented Dec 10, 2020 at 9:05
  • what is your addPanelHtml ? Can you provide more of your code please? Commented Dec 10, 2020 at 9:11
  • I think your error is here: onclick='histDrpDwnEdt("+chkval+"','" + i+ ") you should remove the single quote signs around the comma (if both chhval and i are numeric values): onclick='histDrpDwnEdt("+chkval+"," + i+ ") or you should add one before chval and one afer i (if they are string values): onclick='histDrpDwnEdt('"+chkval+"','" + i+ "'). Commented Dec 10, 2020 at 9:40
  • ... actually, if chhval and i are string values, you should add the single quote signs and escape them: onclick='histDrpDwnEdt(\'"+chkval+"\',\'" +i+ "\') Commented Dec 10, 2020 at 9:56

3 Answers 3

2

Try instead of writting the ', try like this \':

addPanelHtml += "<li><a href=\'#\' id=\'btnhistory" + i+ "\' onclick=\'histDrpDwnEdt("+chkval+"\',\'" + i+ ")\'>History Parameters</a></li>";
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

The suggestion is good but the string you provided is not correct; it should be addPanelHtml += `<li><a href='#' id='btnhistory${i}' onclick='histDrpDwnEdt(${chkval}, ${i})'>History Parameters</a></li>`;
@developerExecutive, actually that is called a template literal or a template string (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…), as correctly mentioned by Alex, while concat is a method of String.prototype (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…) and it has not changed in ES6. ;)
0

My suggestion:

var addPanelHtml = '', i = 0, chkval = 1;

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

console.log(addPanelHtml);

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.