Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda