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

241
Views
How to pass django context values to the javascript variable?

I want to pass all the values of the {{ i.nsn }} turn by turn to the ajax script.

{% for i in dibbs %}

<p>{{ i.nsn }}</p>

{% endfor %}

If I tried the ajax script in the way

{% for i in dibbs %}
  <script>
      var nsn = {{ i.nsn }}
      console.log("Hello")
         $('.togglebtn').on("click",function(){
         console.log("Hello")
         $.ajax({
            type: 'GET',
            url: "http://localhost:8000/toggle/"+nsn,
            contentType: 'application/json',
            success: function (data) {
                  appendData(data);
                   function appendData(data) {
            var mainContainer = document.getElementById("switch_{{forloop.counter}}");
            for (var i = 0; i < data.length; i++) {
                var div = document.createElement("div");
                div.innerHTML = '<tr>' + data[i].line_items + ' ' + data[i].nomenclature+'</tr>' ;
                mainContainer.appendChild(div);
            }
        }
            }
  });
  });




      </script>

{% endfor %}

The ajax script also repeats as per the loop.

My url is different for every nsn. I want to send a single ajax request when I clicked in the button.

I want to execute a single ajax function for a single click removing the for loop.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think you can try to fetch value from p tag and pass it to your ajax

{% for i in dibbs %}
  <p>{{ i.nsn }}</p>


  <script>
    var nsn = ctrl.getElementsByTagName('p')[0].innerHTML;
      console.log("Hello")
         $('.togglebtn').on("click",function(){
         console.log("Hello")
         $.ajax({
            type: 'GET',
            url: "http://localhost:8000/toggle/"+nsn,
            contentType: 'application/json',
            success: function (data) {
                  appendData(data);
                   function appendData(data) {
            var mainContainer = document.getElementById("switch_{{forloop.counter}}");
            for (var i = 0; i < data.length; i++) {
                var div = document.createElement("div");
                div.innerHTML = '<tr>' + data[i].line_items + ' ' + data[i].nomenclature+'</tr>' ;
                mainContainer.appendChild(div);
            }
        }
            }
  });
  });




      </script>

{% endfor %}

You can also hide the p tag if you don't want to display

about 4 years ago · Juan Pablo Isaza 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!