<body>
<h1>Welcome, user.</h1>
<input Id="user_input">
<button onclick="myFunction()">SUBMIT</button>
<h1 Id="message"></h1>
<script>
function myFunction(){
let user_input = document.querySelector("#user_input");
let message = document.querySelector("message");
message.innerHTML = "welcome, user " + user_input.value;
}
</script>
</body>
I have innerHTML javascript problem and is not working. Please help me to find it
html javascript innerhtml
Go to the second line in Javascript Function.
let message = document.querySelector("message");
You are missing the # sign in selecting the message in the query selector
The issue lies in the line where you're selecting the message element. You're missing the # symbol to indicate that you're selecting an element by its ID.
Here are some possible solutions:
innerHTML of an element before the element has been added to the DOM, it won’t work. You can either move your script tag down further or add your logic to a function which ought to be called when the document has been loaded. Here’s an example of how to do this:Check for typos: Make sure that you’re using the correct spelling of innerHTML. It’s case-sensitive and should be written in camelCase.
Use document.querySelector(): If document.getElementById() isn’t working, try using document.querySelector() instead. Here’s an example:
document.querySelector("#ma").innerHTML = "HELLO";
defer keyword: You can also use the defer keyword with a script to execute it after the DOM has been initialized. Here’s an example:<script>
function myFunction(){
let user_input = document.querySelector("#user_input");
let message = document.querySelector("#message"); // Add # before message
message.innerHTML = "Welcome, user " + user_input.value; // Corrected innerHTML
}
</script>
By adding #
while assigning the messsage Id using querySelector the "#" is missing.
because you missed adding the "#" sign in your querySelector message