I want to put the script between the <head> tags using the anonymous function and I add the onclick event to change the contents of the tag p but after I click nothing happens. Is there anything wrong from my script please help me. thank you
<!DOCTYPE html> <html lang="en"> <head> <title>web</title> <script> window.onload=function(){ function rubah1(){ document.getElementById("text1").innerHTML="SUDAH BERUBAH TEXT 1"; } } </script> </head> <body> <p id="text1">Teks 1 Sebelum Di Rubah</p> <button type="button" onclick="rubah1();">Rubah Text 1</button> </body> </html>
rubah()is not in global scope since you wrapped it in another function. Study some tutorials on "javascript scope"