My innertext does not work together with the variable

Asked

Viewed 33 times

-2

<!DOCTYPE html>
<html lang="pt-BR">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Eventos DOM</title>
    <style>
      div#area {
        background: rgb(15, 116, 15);
        font: normal 18pt arial;
        color: white;
        width: 200px;
        height: 200px;
        line-height: 200px;
        text-align: center;
      }

    </style>

  </head>

  <body>

    <div id='area' onclick="clicar()">
      interaja...
    </div>
    <script>
      function clicar() {
        var a = window.document.querySelector('area').innerText = 'clicou'
      }
    </script>
  </body>
</html>

when using onclick was to appear the option to click on Live Server. Can anyone find any error in the code? searched and nothing. Thanks, from now on!

1 answer

0

You are using querySelector, would have to put a # to catch the id in question. Works as a CSS selector, # for id and . for class.

function clicar(){ var a = window.document.querySelector('#area').innerText='clicou' }

Note the #area instead of just area.

  • Thanks, it worked out!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.