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 + " %}" ).
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");