Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing webform from javascript file
    text
    copied!<p>I'm looking for a way to send some hidden form-data to a Django-server, which works out fine as long is a keep the javascript and the html(template) in the same file, but now i'm trying to separate them.</p> <p>Probably really simple, but i guess all i have to do is get(set?) the 'name' of the window/html-and access it from the javascript module?</p> <p>HTML:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script src="/media/postIt.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="" method="post" id="postForm"&gt;{% csrf_token %} &lt;input type="hidden" id="scoreField1" name="score1"&gt;&lt;/input&gt; &lt;input type="hidden" id="scoreFieldSet1" name="score1Set"&gt;&lt;/input&gt; &lt;input type="hidden" id="scoreField2" name="score2"&gt;&lt;/input&gt; &lt;input type="hidden" id="scoreFieldSet2" name="score2Set"&gt;&lt;/input&gt; &lt;input type="hidden" id="scoreField3" name="score3"&gt;&lt;/input&gt; &lt;input type="hidden" id="scoreFieldSet3" name="score3Set"&gt;&lt;/input&gt; &lt;/form&gt; &lt;table&gt; &lt;tr&gt; &lt;td id="bg1" onclick="postIt('score1')"&gt; Ones &lt;/td&gt; &lt;td id="bg1_1"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td id="bg2" onclick="postIt('score2')"&gt; Twos &lt;/td&gt; &lt;td id="bg2_1"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>Javascript:</p> <pre><code> function postIt(x){ var pointArray = [d1,d2,d3,d4,d5]; var totVal = d1+d2+d3+d4+d5; pointArray.sort(function(a,b){return a-b}); alert(pointArray); alert("total value: " + totVal); if(x == "score2"){ if("{{ res.score2Set }}" == 0){ var form = document.getElementById('postForm'); document.getElementById('scoreField1').value = score1; document.getElementById('scoreFieldSet1').value = score1Set; document.getElementById('scoreField2').value = score; document.getElementById('scoreFieldSet2').value = 1; document.getElementById('scoreField3').value = score3; document.getElementById('scoreFieldSet3').value = score3Set; form.submit(); } } </code></pre> <p>Edit: Clarifying: I can't reach the elements in the html 'document' by just calling document.getElement... since they now are in different documents (physically..). I guess i'll have to assign a name/variable to the document, somehow. And the call it like: nameOfDocument.getElement.. </p> <p>and so on..</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload