1

I have the following javascript code that attempts to insert HTML code dynamically. I have an error the syntax but I do not know how to correct it. Please can someone advise?

 loadNextContainer.innerHTML = '<span class="sr_43" id="srloadnext_2" onclick="srt.loadNextMatchRecords(\''+numDownloadSoFar+'\', \''+maxDownloadLimit+'\', \''+folderName+'\', \''+jsonHashTags+'\', \''+fromDate+'\', \''+toDate+'\', \''+lastScanId+'\')">LoadNext</span>';

ERROR MESSAGE:

 Uncaught SyntaxError: Invalid or unexpected token

I believe the error revovles around the function variables. In particular the JSON variable. I cannot change the use of double quotation marks for each element in the JSON. So that has to stay in any solution.

enter image description here

3
  • well you can always use template literal expression in such cases to avoid syntax issues Commented Aug 11, 2018 at 23:07
  • When you are concatenating your strings + variables, you are using single quotes that are opening the string unexpectedly. For example, look at just this piece: jsonHashTags+'\', \''+fromDate. You have single quotes inside your single quotes. Commented Aug 11, 2018 at 23:10
  • @xHocquet Appreciate the reply. But it would really help if you could re-write the line of code in above with the correct syntax. I would be able to better understand. Commented Aug 11, 2018 at 23:13

3 Answers 3

2

You could try it using backticks like this.

loadNextContainer.innerHTML = `<span class="sr_43" id="srloadnext_2" onclick="srt.loadNextMatchRecords('`+numDownloadSoFar+`', '`+maxDownloadLimit+`', '`+folderName+`', '`+jsonHashTags+`', '`+fromDate+`', '`+toDate+`', '`+lastScanId+`')">LoadNext</span>';

Or just escape the double-quotes instead of the single quotes. The way you're doing it, you're unintentionally opening up the string when you try to escape it.

loadNextContainer.innerHTML = "<span class=\"sr_43\" id=\"srloadnext_2\" onclick=\"srt.loadNextMatchRecords('"+numDownloadSoFar+"', '"+maxDownloadLimit+"', '"+folderName+"', '"+jsonHashTags+"', '"+fromDate+"', '"+toDate+"', '"+lastScanId+"')\">LoadNext</span>";
Sign up to request clarification or add additional context in comments.

1 Comment

If you look at the image in his question, one of his parameters is an array. Template literal won't help with that, not without him doing a JSON.stringify or something.
0

Judging from your image of your variables, none of the template literal solutions are going to help you, because at least one of your parameters is an array.

Yes, it would be possible, with enough effort, to convert that array into a string representation that would work with innerHTML. But it's going to be very difficult. You'd end up needing to use JSON.stringify to get the string version of your array at least.

An easier solution is actually going to be to create the element and then add the onclick operator through pure javascript, like this:

var s = document.createElement('span');
s.className = 'sr_43';
s.id = 'srloadnext_2';
s.innerText = 'LoadNext';
loadNextContainer.appendChild(s);
s.onclick = function(e) {
  srt.loadNextMatchRecords(numDownloadSoFar, maxDownloadLimit, folderName, jsonHashTags, fromDate, toDate, lastScanId);
};

2 Comments

I used your code above but it failed to incorporate the onclick attribute in the span element? I even tried placing the s.onclick line before adding loadNextContainer.appendChild(s);.
I can't imagine why it wouldn't be attaching the listener; perhaps it's a javascript error rather than a failure to attach? Have you checked the browser console for errors?
0

Try it with template literals:

loadNextContainer.innerHTML = `<span class="sr_43" id="srloadnext_2" onclick="srt.loadNextMatchRecords(\'${numDownloadSoFar}\', \'${maxDownloadLimit}\', \'${folderName}\', \'${jsonHashTags}\', \'${fromDate}\', \'${toDate}\', \'${lastScanId}\')">LoadNext</span>`;

Edit: As others already mentioned. Sorry.

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.