The DOM API you are looking for is: getElementsByClassName
onclick needs to be a function that will be called on click event. What you are doing is calling innerHTML='zufall'
immediately, returning the string 'zufall'
, and assigning onclick='zufall'
, which is not what you want.
JavaScript array literals are denoted with [] not ().
I'm assuming the string 'zufall' is not what you want, but the variable zufall, which I've adjusted by removing the single quotes.
Math.random() (not random()
) returns a random number from 0 to 1. Multiply that by the length and then floor to integer (You could use Math.trunc() also). And then in brackets to access that index in Orte.
Again, variables are set when they are assigned. Which is probably not what you want here. The same thing happens in Python. zufall would be assigned a value that doesn't change unless something is called to update it. This is a common mistake, although as an experienced Python programmer you should not be making this mistake anymore.
I've adjusted it so it updates zufall on every click. As you should be able to see, a global variable is unnecessary. You can just generate the zufall only in the onclick handler.
var Orte = ['a', 'b', 'c']
var zufall = Orte[Math.random()*Orte.length | 0]
button1.onclick = function(){
zufall = Orte[Math.random()*Orte.length | 0]
document.getElementsByClassName('text')[0].innerHTML = zufall
}
<div class="text"></div>
<button id="button1">click</button>