0

I am encountering a problem with a script. The solutions to the previous answered questions did not work out for me.

So, basically, it does not work on the webpage itself, but it does work on jsfiddle.

This is the input field:

<div id="input-field" class="input-field editable" type="text" contenteditable="true" data-ph="Type here to send a message" g_editable="true" role="textbox" style="display: inline-block; border: none; height: 2.7375em; padding: 0.3em 0.3em 0.3em 0.6em; width: 1007px; font-weight: bold; font-style: italic; font-family: 'Comic Sans', 'Comic Sans MS', sans-serif; font-size: 200%;">document.getElementsByClassName("input-field")[0].textContent = 'hi';</div>

I want to edit the value of it with the following code:

document.getElementsByClassName("input-field")[0].textContent = 'Testcode';

However, when I put that in Chrome, I am getting this error: Uncaught TypeError: Cannot set property 'textContent' of undefined

I was told to use window.onload, or to place the script as last child of the body.

How do I manage to do that in Chrome console?

Thank you.

hellobro
  • 21
  • 1
  • 1
  • 5
  • document.getElementsByClassName("input-field")[0].textContent = 'Testcode'; returns what seems like an array, but its not, you need to convert to an array... – omarjmh Apr 09 '16 at 20:22
  • _"However, when I put that in Chrome"_ What do you mean by "put that in Chrome" ? – guest271314 Apr 09 '16 at 20:25
  • I mean Chrome dev Console. – hellobro Apr 09 '16 at 20:26
  • Deleting a closed question to ask a new identical one is not OK. If you think your question is different than [Why does jQuery or a DOM method such as getElementById not find the element?](http://stackoverflow.com/q/14028959/1529630), you must explain how. Otherwise I will keep closing as duplicates. – Oriol Apr 09 '16 at 20:26
  • 1
    To clarify, the cause of the problem might be different indeed, but you didn't include enough information in your question to be able to know what's happening. We only know the `getElementsByClassName` DOM method does not find your element. So your question is either a duplicate of that one, or otherwise does not include the shortest code necessary to reproduce the problem in the question itself, and thus is off-topic. See [ask] and [mcve]. – Oriol Apr 09 '16 at 20:37
  • When do you call `document.getElementsByClassName("input-field")[0].textContent = 'Testcode';` at `console` ? – guest271314 Apr 09 '16 at 20:51

0 Answers0