-1

This should be super simple.

Using Vanilla JS, I am simply attempting to update the innerHTML of a span element with whichever session ID I am getting back from a function.

See example below :

sessionId = 0_77b1f7b5-b6c8-49a0-adbc-7883d662ebba
document.getElementById("sessionID").innerHTML = sessionId

However, running it does not have any effects from my HTML/JS files.

Running the above in the Browser Console returns (in Firefox, but same happens in Chrome):

Uncaught SyntaxError: numeric separators '_' are not allowed in numbers that start with '0'

I do need this session ID as it is (0 and underscore and all).

I looked at the Firefox Doc (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Identifier_after_number) and found that adding '' like '0_77b1f7b5-b6c8-49a0-adbc-7883d662ebba' then work.

However, when I receive this data, I cannot seem to be able to String() it.

Object { sessionId: "0_77b1f7b5-b6c8-49a0-adbc-7883d662ebba", modules: (1) […] }

sessionId = String(setupCompleteData.sessionId)

console.log("TEST : ", sessionId)

TEST : 0_77b1f7b5-b6c8-49a0-adbc-7883d662ebba

Would you have any idea how to go around this?

Thanks a lot !

Pytharelle
  • 73
  • 7
  • 2
    try `sessionId = "0_77b1f7b5-b6c8-49a0-adbc-7883d662ebba"`, in JS a string is surrounded by quotes (`'` or `"`). – Ulysse BN Mar 10 '21 at 16:03
  • Try [What's the best way to convert a number to a string](https://stackoverflow.com/q/5765398/2873538) – Ajeet Shah Mar 10 '21 at 16:05
  • 1
    in addition one wants to write `myElementNode.textContent = sessionId;` instead of `myElementNode.innerHTML = sessionId;` – Peter Seliger Mar 10 '21 at 16:06
  • @Peter Seliger Thanks for the tip, I was not aware of .textContent. – Pytharelle Mar 10 '21 at 16:27
  • @Ulysse BN Adding quotation marks work, but the issue is that they are not provided in the first place within the Node. – Pytharelle Mar 10 '21 at 16:40
  • @Ajeet Shah I tried sessionId.toString() but receive the same error It seems impossible to stringify any int/sessionsIds starting with 0? – Pytharelle Mar 10 '21 at 16:40
  • 1
    @Pytharelle There are 23 answers in that post and many ways to convert a number into string. Have you tried all? I still don't understand why it is challenging? Can you create a jsfiddle / codesandbox / code snippet to reproduce the scenario and error? (because I am not able to reproduce this error) – Ajeet Shah Mar 10 '21 at 16:46

1 Answers1

0

Turns out, there is no need to stringify the sessionID since it is already of type string ...

Passing this variable straight to textContent did the job. Just so happen that of course the span would not update with .value.

Solution : document.getElementById("sessionID").textContent = setupCompleteData.sessionId

Pytharelle
  • 73
  • 7