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

272
Views
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 answers
Answer question

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