I have an HTML container like this:
<div class="col-6">
<div class="card ">
<div class="card-header py-2">Container Left</div>
<div id="leftContainer" class="list-group" style="height:225px; overflow-y: scroll">
{% for item in item_history %}
<a href="#" data-set="5000" id = "{{forloop.counter}}" class="list-group-item py-0 list-group-item-action">{{ item }}</a>
{% endfor %}
</div>
</div>
</div>
<p id = "output"></p> <!-- For DEBUG output -->
And I have a JS like this of which half is mine. I am able to print the value in the var item_selected
back to the HTML page where the <p id="output>"
is at.
<script>
$("#leftContainer > a").click(function(event){
event.preventDefault();
$("#leftContainer > a").removeClass("active");
$(this).addClass("active");
var leftDataSet = parseInt($(this).attr("data-set"));
var item_selected = $(this).text();
document.getElementById("output").innerHTML = item_selected
// Found this while googling, supposed to send data back to Python.
var xmlhttp;
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
// xmlhttp.onreadystatechange=function(){ // really not sure what this block does?!
// if (xmlhttp.readyState==4 && xmlhttp.status==200){
// document.getElementById("output").innerHTML=xmlhttp.responseText;
// }
// }
xmlhttp.open("GET",item_selected,true); // just send item_selected
xmlhttp.send();
});
</script>
I want to send the selection from the list box/container upon selection, which is currently stored in the JavaScript in the variable item_selected
. I want to send this info back to Python which renders this page/template.
Can you help; I know Python 3, suck at JS/JQuery. My Framework used is Django. I render this page from the backend using render
method like this:
# myapp/views.py
def render_view_results(self, request, projectid):
return render(request, 'myapp/index.html',{'item_history':items})
Also, I do not want to send info back from HTML/JS to Python using the GET method suggested here