3
I am making a script so that when client click on one of the colors that the product has.
the product next to change the photo. However, as it is a precise photo array that each id contains a different name. Then inside the product loop add the photo the id:
id="trocarimg{{$id}}"
Problem
I can’t get him to take this id with the complement of the variable ex: trocarimg1 - trocarimg2 ... My javascript code is as follows:
here where I call the variable Document.getElementById("trocarimg") would need that in the trocarimg the same code appeared as the variable inside the array. If you need more information, just ask. Thank you.
Have you tried
document.getElementById("trocarimg"+i)
?– Sam
Hi Sam, I think I’ve even tried. It returns me the following error Uncaught Typeerror: Cannot set Property 'src' of null I know it must be something very small but since I’m not very good of JS (I’m studying) I can’t see the problem.
– adrib
is executed before or after the elements? If it is before, it will actually give that error that you even quoted.
– Sam
Displays the code that creates/defines the element with the class
trocarimg
– matheus ale da silva
Another thing, don’t use a function inside a loop that doesn’t work.
– Sam
For tests I left everything on the same page, for example. I have all html+php above this javascript code. I don’t know if that answers your question. Here’s the full code link
– adrib
Hello Matheus, I’ve created a Pastebin for this. code, thanks for the reply for now.
– adrib