-3

$( "#target" ).click(function() {
    var x = document.getElementById("myanchor").href;
});
<div><a href="{{ audio.get_api_url }}" ><i class="icon-control-play i-2x" id="target"></i></a> </div>

I need to send the api url to the my jquery function to call the ajax function to execute the code further. The page should not redirect to the api url, I cant figure this thing out.

Baked
  • 1
  • 1
  • I was trying to use DOM, `
    ` but it redirects the page to my api call
    – Baked Jun 22 '18 at 17:28
  • This code is sort of all over the place. The purpose of Django is to prepare the variables and data which you will use in a webpage. AJAX is a technology you use to obtain information by a client once it's rendered the webpage. I would suggest first spending some time getting a static web page to work with an ajax call, then work on binding the variables to your Django template player. Here's just one article on making AJAX calls: https://www.w3schools.com/jquery/jquery_ajax_intro.asp – Gerik Jun 22 '18 at 17:38
  • There is no `id="myanchor"` in your snippet – Taplar Jun 22 '18 at 17:43

1 Answers1

0

The question should be simply "how to avoid redirection on anchor tag and call function", how is it related to DJango templates ?


Anyway, you can avoid redirecting using JQuery's preventDefault() method. Here is the example:

$( "#target" ).click(function(e) {
    e.preventDefault();
    var x = document.getElementById("myanchor").href;
});

Checkout the answer here for further explanation.

dedman
  • 868
  • 6
  • 16