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