Passing docname to jinja from js

1) I try to use frappe api in jinja as mentioned in this Jinja API doc

{% set doc = frappe.get_doc('Task', 'TASK00002') %}

But since the name of the document is from a list (generated with jinja for-loop), I get it with onclick function.

Question:
How do I pass the onclick result to the jinja? something like:

{% set doc = frappe.get_doc('Task', docname) %}

2) Second option is to use frappe.call in the <script> to get the doc in server side and use callback for the value.
But always gets error:

ReferenceError: Can't find variable: frappe

Question:
How do I resolve this?

Thank you