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

156
Visualizações
How to have click events for iterated links

I have an asp.net app, on the app's dashboard the names of websites are pulled through to the view from a SQL DB like this.

 <h1>My Websites</h1>

 

  

 @{

        foreach (var website in Model.websites)
        {
            <table>
                <tr>

                    <td>  <a id="websiteLink" href="">@website </a> </td>

                </tr>

            </table>
        }

}

which works fine:

pic of websites pulled from db

I am trying to have a click event for each of the website links, in order to pull info based on the clicked website.

 <script>

        var website_link = document.getElementById("websiteLink");

        website_link.addEventListener("click", function () {
//test the click event
            alert("link clicked");
        })

       

    </script>

But the click event only works on the first link? when I inspect the page at runtime with chrome devs tools, the correct ID is added to the second link but the click event does not fire? Please help me.

My goal is to render information based on the clicked link, I basically just need the html.value of the clicked link to complete my task...

Thank you in advance for any suggestions!

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

0

My solution :

document.querySelectorAll('.websiteLink').forEach(item => {
  item.addEventListener('click', event => {
    alert(item.innerHTML);
  });
});

From here, How would I be able to access this item.innerHTML value in my controller or model ?

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