+ 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>
4 Answers
+ 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>"
}
+ 2
You have a few errors, perhaps these threads will help you:
https://www.sololearn.com/discuss/1128900/?ref=app
https://www.sololearn.com/discuss/1103814/?ref=app
https://www.sololearn.com/discuss/301642/?ref=app
+ 1
Thanks guys
+ 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