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