+ 3
why iam getting an error in thies code ?
Iam trying to pass a value to inputbox when user clicks hello but it is not working.Iam new to js so can u pls help me https://code.sololearn.com/WyDb4z2UnxqZ
9 Answers
+ 5
V-starš Write js code inside this
window.onload = function() {
}
+ 5
It works if you follow AJ Anant instructions
onload = () => {
const a =document.querySelector(".item");
a.addEventListener("click", () => {
//.... Your code
}
}
+ 3
V-starš Not showing any error. Can you show what you did?
Check here
https://code.sololearn.com/WCPtwhL29Orw/?ref=app
+ 3
Thank you all for your help š
+ 2
Still it's showing some error
+ 2
I don't know if I'm correct of this, but it also seems like you're listening for an event, but not calling anything after it. You could try creating a function, then calling ir after addEventListener...
Then also wrap your code inside a 'window.onload(){}' or 'window.addEventListener("DOMContentLoaded", init(){}' where init is the name of the function you want to execute
+ 2
you also have no button in your HTML
+ 2
Joshua Flynn Doesn't matter because he is clicking on Hello word.
+ 2
Your HTML
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div class="item">
<label>helo</label>
</div>
<input type="text" id="mytext"/>
</body>
</html>
/* if what you want is to show
the content of the input box on the div when you click on the div, try this code */
const a =document.querySelector(".item");
a.addEventListener("click", () => {
a.innerHTML = document.getElementById("mytext").value;
});