0

I have seen so many responses but none of those correspond to my needs. Basically I am trying to get the value of an href through JavaScript and I need to pass that to my django views, when I do a console.log this is the output that I am getting

http://localhost:8000/#vert-tabs-Personal localhost:8000:316:21
#vert-tabs-Personal localhost:8000:317:21
http://localhost:8000/#vert-tabs-Career localhost:8000:316:21
#vert-tabs-Career localhost:8000:317:21
http://localhost:8000/#vert-tabs-Finances localhost:8000:316:21
#vert-tabs-Finances

And here is the script that triggered the output so far

    <script>
    $(".nav-link").on('click', function(e){
        console.log(e.target.href);
        console.log(e.target.hash);
    });
</script>

Now What is the best way to get the e.target.hash value passed to my django views. I am thinking of jquery or ajax but honestly I don't know. Any help would be appreciated. Thank you in advanced...

Sahine
  • 61
  • 8
  • on the click event use jquery ajax to call the href and the ajax success method update the html here is the documentation about ajax in jquery https://api.jquery.com/jquery.ajax/ – Dimitris Kougioumtzis Jun 15 '21 at 08:40

1 Answers1

0

Your question is a little vague as to what you want to achieve, but I'd say there are a couple of ways:

  1. Via a form

presumably you have some kind of form on this page. If you just need the value in your django view when you submit the form, then include an input tag (which can be hidden, so nobody sees it), then update the value of this via Javascript as and when you need to. This value will then be received by the view as it will be part of the submitted form.

See here for setting an <input> value : How to set the value of a input hidden field through JavaScript?

  1. Via an ajax call

You could have an endpoint to send this to, where you could just stuff it into the body of the ajax call, and this could potentially be part of the view you're working with, though usually it's better to separate these things out and have specific views to handle ajax stuff (or at least that's my experience).

Also, since you say you want this "in your view", I'd suggest option 1 is the simplest and cleanest option for you.

Add that <input> tag to your template, make it hidden, and use JS to set the value of it, and presto, you'll see it in your view when you handle the submit.

michjnich
  • 2,796
  • 3
  • 15
  • 31
  • No, the value may be coming from an anchor tag, but to pass it back to the django view, you want to set that value into a form field, for which an `input` is the best choice. Though I do question how you are setting a URL in the first place ... – michjnich Jun 15 '21 at 10:06
  • As I mentioned above this hrefs aren't for pages links but they display a certain content depending on the user's click link, they are just bootstrap navbar tabs content. When it comes to URL they are located in the urls.py django file as normal I guess. But these one use a hash followed but the id. And I am wondering how to wrap them in an input – Sahine Jun 15 '21 at 11:30
  • Here's a seperate question and response on how to set a hidden input field value: https://stackoverflow.com/questions/19232822/how-to-set-the-value-of-a-input-hidden-field-through-javascript – michjnich Jun 15 '21 at 11:54