+ 1

Hi everyone! I want change div with name "cd" to text(how to add url here)when I click button. No my code not worked. help me.

<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <div id="demo"> My first JavaScript </div> <div id="cd"> <p> My second JavaScript </p <p > My third Javascript </p> </div> <button onclick="fff()">click</button> <script> document.getElementById("demo").innerHTML = "Hello World"; function fff() {documemt.write("dadaxon")} document.getElementById("cd").innerHTML=fff() </script> </body> </html>

26th Apr 2024, 9:06 AM
Dadaxon Xudayberganov 👑 🇺🇿🇺🇿
Dadaxon Xudayberganov 👑 🇺🇿🇺🇿 - avatar
4 Réponses
+ 4
Dadaxon Xudayberganov 👑 🇺🇿🇺🇿 , you have a typo in line 28: 👇 {documemt.write("dadaxon")} and line 15: </p document.write refreshes the entire page, not any part of it, so the function must immediately record a reference to a specific element of the html document: function fff(){ document.getElementById("cd") .innerHTML="<p>dadaxon</p>" }
26th Apr 2024, 1:24 PM
Solo
Solo - avatar
26th Apr 2024, 11:12 AM
Ausgrindtube
Ausgrindtube - avatar
+ 1
Thanks guys
26th Apr 2024, 9:11 PM
Dadaxon Xudayberganov 👑 🇺🇿🇺🇿
Dadaxon Xudayberganov 👑 🇺🇿🇺🇿 - avatar
+ 1
Dadaxon Xudayberganov 👑 🇺🇿🇺🇿 I noticed your question about how to add the url on click wasn't answered. When naming it is helpful to give meaningful names. I choose a meaningful name such as addURL. You may use the innerHTML property to update the HTML content of the #cd div. Same as you did with Demo To complete the task use an anchor tag (<a>) to add the URL. <button onclick="addURL()">Click</button>   <script>     function addURL() {             document.getElementById("cd").innerHTML = "<a href='https://www.sololearn.com'>Welcome to Sololearn</a>";         }   </script> https://sololearn.com/compiler-playground/W7sKYpU5Cd7f/?ref=app
27th Apr 2024, 10:28 PM
Chris Coder
Chris Coder - avatar