Hi everyone please suggest me how to break
for loop in jinja 2
I am using for
loop with if
condition and I want to break the loop if my if
condition is true
this is my code
function refercheck(){
var input_value = document.getElementById('refer').value;
{% for i in refer %}
document.getElementById("valid").innerHTML = '';
if( input_value == "{{i.refercode}}" ){
$('#valid').append('Referred By {{i.username}}');
}
I am using this function in input onkeyup="refercheck()"
and I an sending the dict
from view function. My dict key is refer I am sending all user data and checking the input is same as other user refercode. The above code is give me only last row refercode is same please tell me how can I break for loop when my if condition is true or any other suggestions for it.
According to this answer, you can add the condition within the for loop statement
Simply:
function refercheck(){
var input_value = document.getElementById('refer').value;
document.getElementById("valid").innerHTML = '';
{% for i in refer if input_value == i.refercode %}
$('#valid').append('Referred By {{i.username}}');
{% endfor %}
Update: Jinja2 does not support break or continue statments
you have to use other ways to solve this problem
here is what came to my mind I didn't try the code but here is the idea.
function refercheck(){
var input_value = document.getElementById('refer').value;
refer = {{refer|safe}}
for (i = 0; i < refer.length; i++) {
document.getElementById("valid").innerHTML = '';
if( input_value == i.refercode ){
$('#valid').append('Referred By '+i.username);
break;
}
}
}
if i.refercode
and i.refercode
does not work try i['refercode']
and i['refercode']