Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
How to execute a <script> tag in a <a> element in html?

Heres my code:

<div>
   <a onclick="settings()">
     <script type="text/javascript" src="popup.js">
     <img src="icons/gear.png"
   </a>
</div>

I wanna keep the icon for the button

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can create an EventListener which will listen when someone clicks on the anchor tag (<a></a>). So to do this, first let's give our anchor a class name, for this example:

<a class="example">Click Me</a>

Now, in our javascript file, we need to select this element by its class name. To do that we need to:

  1. Define a variable (myVariable)
  2. Select the anchor tag and assign it to that variable.

So:

var myVariable = document.querySelector('.example');

We created a variable and named it myVariable. We use document.querySelector('.example') to select our anchor element by its class name and assign it to our variable.

Now we need to create an EventListener:

myVariable.addEventListener('click', () => {
    // some code to be executed
})

Here you can see that we used the variable followed by a dot (.) and addEventListener().

Q: Why did we use the variable name? A: We use the variable name before .addEventListener because we actually want to "listen" when someone executes a specific event on our anchor tag, in this case the "click" event.

Note: You should never put inside some div element.>ou always add them to the bottom of the page, before the closing </body> tag.

So it should look like this:

</body>
<!--Some Code-->

<script src="example.js"></script>
</body>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda