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

242
Vistas
How to append link to <a> element using Javascript

I want to add href to an a html element from class of another element. It might also be good to know that i am using Django.As well i dont want the second url to be visible, when the character is not selected yet. Firstly i have this list:

<ul>
  <a class="1" id="tablink">A</a>
  <a class="2" id="tablink">B</a>
...
</ul>

if list from the first ul is selected, i want the class of the list (example: A) to add to href element to all links in another ul:

<ul>
<a href="{% url id=id character= <!--here i want the number selected to be inserted--> %}">One</a>
<a href="{% url id=id character= <!--here i want the number selected to be inserted--> %}">Two</a>
...

</ul>

Thank you! EDIT: I have this code now:

<script type="text/javascript">

$(".tablinks").on("click", function(){
  var char = this.getAttribute("id");
  document.getElementById("subjects").href = "{% url 'subject_id' letnik_id=letnik_id classes_id=classes_id subject_id=" + char + " %}";

  });
</script>

But it has a problem: It only works if i add attribute to element that i got with ID, so id doesnt work with

<a class="subjects">Hello</a>
doucment.getElementsByClassName("subjecs").href = ...

and the DJango url doesnt work ("{% url 'subject_id' letnik_id=letnik_id classes_id=classes_id subject_id=" + char + " %}").

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

For giving the anchor a href use that:

$("#yourElement").setAttribute("href", "http://yourlink.com");

And if you're using JavaScript:

document.getElementById("yourElement").href="http://yourlink.com"";

You disable the second anchor with that code, change id and class value, you can't have the same id's - id are unique values:

$(".tablink").on("click", function(){
  $(".tablink").css("display", "none");
  $(this).css("display", "block");
});

To add href when link is clicked simply add to the bottom of the above function:

$(this).setAttribute("href", "http://yourlink.com");
over 4 years ago · Santiago Trujillo 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