Skip to main content

Innerhtml JavaScript Not Working

Published: Asked By 6 Answers Answered
  <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

Share:

6 Answers

S
Sadique Community Contributor Answered on

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

L
LAZIO LEO Community Contributor Answered on

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.

S
Sooraj Community Contributor Answered on

 Here are some possible solutions:

  1. Wait for the DOM to load: If you’re trying to update the 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:
    window.onload = function () {
      // Add your logic here
    }


     
  2. Check for typos: Make sure that you’re using the correct spelling of innerHTML. It’s case-sensitive and should be written in camelCase.

  3. Use document.querySelector(): If document.getElementById() isn’t working, try using document.querySelector() instead. Here’s an example:
    document.querySelector("#ma").innerHTML = "HELLO";

     

  4. Use 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 type=\"text/javascript\" defer>
      // Your script here
    </script>


     
A
Alex TW Community Contributor Answered on

<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 #

A
Ameen Community Contributor Answered on

while assigning the messsage Id using querySelector the "#" is missing.

S
Sahadha Community Contributor Answered on

because you missed adding  the "#"  sign in your querySelector  message

Your Answer

Other Resources

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

Q&A Image
Q&A

Ask your coding-related doubts and get answers.

Certification Image
Certification

Earn certifications to enhance your resume.

internships Image
Internships

Hands-on projects to improve your skills.

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

blog Image
Blogs

Add your technical blogs and read technical topics.

Certification Image
Certifications

Earn certifications to enhance your resume.