There are lines with text, I want to change the color in it:

bgimg.replace("grey","white"); 

but in the end I get the error main.js: 8 Uncaught TypeError: Cannot read property 'replace' of undefined (...)

How can I replace the substring with another without any problems?

  • And why not just change the type property: bgimg.colour = "white"; ? And it can not work because bgimg is not text, if there is exactly text there you can make bgimg.toString (). Replace ("gray", "white"); - pnp2000
  • What is bgimg ? What does typeof bgimg ? - Anton Shchyrov
  • one
    @AntonShchyrov, obviously - undefined . - user207618
  • You are not at the line trying to replace the text. - user207618
  • (bgimg || '').replace("grey","white"); - nörbörnën

1 answer 1

As @Other correctly pointed out, you should check that bgimg is defined in the current scope and inherits the replace method from String. Judging by what you specified, your bgimg is not defined and therefore the replace method cannot be applied to undefined.