Home > OS >  Why does it show the tag's type when I use getElementById method in javascript and html
Why does it show the tag's type when I use getElementById method in javascript and html

Time:09-11

I used getElementById method to return the tag's element but it returns the tag's type ?!

<!DOCTYPE html>
<html>
  <head>
    <title>Title</title>
  </head>
  <body>
    <p id ="textlabel"></p>
  
    <script>
      function bonef(){
        var a = 
  document.getElementById("textlabel");
        var b = "1";
        a.innerHTML  = b;
      }
    </script>

    <button onclick="bonef();">1</button>

  </body>
</html>

It shows "[object HTMLParagraphElement]1" but I want "1" !

CodePudding user response:

Don't use getElementById any more, Instead use the querySelector:

let label = document.querySelector("#textlabel");

I think this is what you want to do:

function bonef(){
   let a = document.querySelector("#textlabel")
    a.innerHTML  = "1";
  }
  • Related