0

I'm using prestashop as a cms and i want to display specific products on a page.

I found a module to do so by writing for example this line of code : [ph-product-cms id="1"] which will be converted at the execution into many lines of code with multiple quotes to display the products

The thing is when i try to use innerhtml like this : document.getElementsByClassName('test0 prod')[0].innerHTML = "[ph-product-cms id="1"]"; it shows Uncaught SyntaxError : Unexpected identifier

this is from the console of the browser

I tried single quotes but it doesn't work either.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Karim
  • 1
  • 2
  • @bergi This is actually a case of not knowing what to ask rather than a dupe (or at least give correct dupe answers on innerHTML vs innerText). Karim: With innerHTML, it expects a proper HTML. So either set HTML code to it or use innerText which is used to set text string to the element. – Ethan Doh Apr 04 '22 at 14:02
  • @EthanDoh I don't see how this is about `innerHTML` vs `innerText` (which no one should be using anyway, maybe you meant `textContent`). It's clearly about the SyntaxError from using quotes in a string literal. But you might be right - there could be a different question hidden underneath, about some prestashop-specific templating syntax? – Bergi Apr 04 '22 at 14:08
  • @Bergi https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText "Note: innerText is easily confused with Node.textContent, but there are important differences between the two. Basically, innerText is aware of the rendered appearance of text, while textContent is not." So similar but with a difference. I would say use innerText unless you know you want textContent. – Ethan Doh Apr 04 '22 at 14:15
  • @Bergi I initially thought it was a case of not knowing escaping double quotes but what caught me was that he said using single quote didn't solve the issue either. – Ethan Doh Apr 04 '22 at 14:17
  • @EthanDoh "*As a setter this will […] convert any line breaks into
    elements.*" - that's usually not what you want.
    – Bergi Apr 04 '22 at 14:20
  • actually this has nothing to do with innerhtml vs innertext . it's just i can't find a way to escape all the double quotes when the initial line of code is converted because before the execution of the script the conversion isn't done yet. i hope i made myself clear cuz it's a bit tricky to explain. thanks in advance. – Karim Apr 04 '22 at 14:42
  • 1
    @Bergi Yes. So one really has to read doc and decide which one to use. – Ethan Doh Apr 04 '22 at 15:22
  • Looking at the question again, it looks like we all got this wrong. I think you need to post the actual code. What I'm seeing is JS syntax error. `innerHTML = "
    ` is a wrong syntax on string concatenation but I'm not seeing the whole code but a screenshot of the error message that doesn't show everything so I can only guess.
    – Ethan Doh Apr 04 '22 at 15:31
  • to elaborate further, that's indeed a wrong syntax for Javascript because it's html that's why i used innerhtml. Next, it's not me who wrote that syntax but the module did , in other terms , the module just converted this code **[ph-product-cms id="1"]** into this one **"
    ** . i confirmed that it's a quote problem because when i replaced double quotes with backticks , there was no erros but the code lacked some lines
    – Karim Apr 04 '22 at 16:43
  • @Karim Can you link the docs of that module, pleaes? – Bergi Apr 04 '22 at 17:16
  • @Bergi here is the link of the module [link] (https://prestahero.com/design-navigation/165-products-on-cms-page-or-anywhere.html) , thank you . – Karim Apr 04 '22 at 17:47

1 Answers1

0

Wherever you find double-quotes put a \ before them to escape them. For example,

\[ph-product-cms id=\"1\"]

  • actually the quotes in this line aren't the problem, when this line will be converted at the execution of the script , it will transform into multiple lines of html code in which there are many double quotes and i can't put the backslash because the code isn't executed yet – Karim Apr 04 '22 at 14:08