Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
Cómo agregar un enlace al elemento <a> usando Javascript

Quiero agregar href a un elemento html de la clase de otro elemento. También podría ser bueno saber que estoy usando Django. Tampoco quiero que la segunda URL sea visible, cuando el carácter aún no está seleccionado. Primero tengo esta lista:

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

si se selecciona la lista de la primera ul, quiero que la clase de la lista (ejemplo: A) se agregue al elemento href a todos los enlaces en otra 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>

¡Gracias! EDITAR: Tengo este código ahora:

 <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>

Pero tiene un problema: solo funciona si agrego un atributo al elemento que obtuve con la identificación, por lo que la identificación no funciona con

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

y la url de DJango no funciona ( "{% url 'subject_id' letnik_id=letnik_id classes_id=classes_id subject_id=" + char + " %}" ).

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Para darle al anchor un uso href que:

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

Y si estás usando JavaScript:

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

Deshabilita el segundo ancla con ese código, cambia la identificación y el valor de la clase, no puede tener las mismas id : las id son valores únicos:

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

Para agregar href cuando se hace clic en el enlace, simplemente agregue al final de la función anterior:

 $(this).setAttribute("href", "http://yourlink.com");
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!