0

Sample 1:

var someText = "Ein Hund kam in die Küche und stahl dem Koch ein Ei.";          // Some Strings      
var moreText = "Da nahm der Koch den Löffel und schlug den Hund entzwei.";

var someNumber = 34;                                                            // Some Numbers
var anotherNumber = 98;
         

function testFunc(){
    var box_uno = document.getElementById("output1");       // "output1" references a <textarea>
    box_uno.innerHTML = someText;
}

Sample 2:

var someText = "Ein Hund kam in die Küche und stahl dem Koch ein Ei.";          // Some Strings      
var moreText = "Da nahm der Koch den Löffel und schlug den Hund entzwei.";

var someNumber = 34;                                                            // Some Numbers
var anotherNumber = 98;
         
var box_uno = document.getElementById("output1");       // "output1" references a <textarea>

function testFunc(){
    box_uno.innerHTML = someText;
}

Sample 1 works fine. The string is displayed in the textarea as expected.

Sample 2 doesn't work and I'd like to fully understand why. I thought variables which are declared outside of a function have global scope. I don't get why I can access the variables with strings and numbers from inside the function, but not the variable containing a html-element.

MaxB
  • 1
  • 3
  • 1
    If you want to set the value of a textarea, you should use `.value`, not `.innerHTML`. `.innerHTML` won't work when the value has previously been populated, eg by user input. – CertainPerformance Mar 06 '21 at 17:36
  • Most likely related to [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/q/14028959/1048572) – Bergi Mar 06 '21 at 17:38

0 Answers0