[Answered ]-Accessing webform from javascript file

2πŸ‘

βœ…

I would guess that the line

if("{{ res.score2Set }}" == 0)

is the problem – an included Javascript file will not go through the Django template loader, so you cannot place variables inside it. This conditional will always evaluate to false.

You need to set the variable in the template file in some manner and then retrieve it in the Javascript. The best way to do this would be probably be to add res.score2Set as an additional argument to the postIt() function, and then do:

<td id="bg1" onclick="postIt('score2', '{{ res.score2Set }}')">

Although obviously I don’t really know what your application does so this might not be applicable. If res.score2Set is used elsewhere in the HTML it might be better to load it in from the DOM in the Javascript.

πŸ‘€Ian R.B.

0πŸ‘

Your assumption about javascript is wrong.Wherever you write JS codes (inline,within file,external file) all JS codes will have access to the DOM.So, the problem is you have not loaded your JS file properly.

πŸ‘€Vivek S

Leave a comment