0

I have a function in JavaScript

function functioncalled(){
var result = 1;
var activityID = {{ mynames[result][8] }};
console.log(activityID);

}

mynames is a list of lists in Flask template. The above code fails giving me an error - UndefinedError: list object has no element Undefined. But when I access the list element using var activityId = {{mynames[1][8]}} everything works fine. How do I make this work? Is there a workaround? I couldn't find the solution anywhere.

1 Answer 1

1

You will not be able to do that, because {{any_flask_variable}}is evaluated on server side and rendered HTML is sent to browser where all javascript runs. At the time of evaluation on the server there is not browser to run javascript code. The best way to resolve this is to assign the list variable to var result.

function functioncalled(){
var result = {{mynames}};
// Here you can access the variable result
console.log(activityID);
}

That off course after assuming that mynames variable can be mapped to js array of arrays.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.