Syntax error in scripting HTML using javascript
Clash Royale CLAN TAG#URR8PPP
Syntax error in scripting HTML using javascript
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.
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.– xHocquet
Aug 11 at 23:10
jsonHashTags+'', ''+fromDate
@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.
– jamesMcKey
Aug 11 at 23:13
3 Answers
3
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.
template literal
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.
innerHTML
JSON.stringify
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);
;
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);.
– jamesMcKey
Aug 12 at 0:04
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?
– David784
Aug 12 at 0:14
-ignore above. you are correct. sorry
– jamesMcKey
Aug 12 at 0:22
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>";
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.– David784
Aug 11 at 23:41
JSON.stringify
@David784 is correct.
– jamesMcKey
Aug 11 at 23:46
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.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
well you can always use template literal expression in such cases to avoid syntax issues
– George Bailey
Aug 11 at 23:07