0

Javascript cannot find element by id using {{key}}

How can I access the {{key}} in my script?

Script

window.onload = jQuery(function ($) {
    var duration = document.getElementById("#timerseconds{{key}}").value;
    duration = 10800 - duration;
    display = $("#counter{{key}}");
    startTimer(duration, display);
});

HTML / DJANGO

{% for key, value in list_cooking.items %}

 <li class="list-group-item">
 <p>{{key}}</p>
 <br></br>
 <p>Seconds:</p><p id='counter{{key}}'>00</p>
 <input id="timerseconds{{key}}" type="hidden" value="{{value}}">
 </li>

Console

jQuery.Deferred exception: Cannot read property 'value' of null TypeError: Cannot read property 'value' of null

jquery.min.js:2 Uncaught TypeError: Cannot read property 'value' of null
    at HTMLDocument.<anonymous> (main.js:78)

Rendered HTML

<li class="list-group-item">
   <p>Beef</p>
   <br>
   <p>Seconds:</p>
   <p id="#counterBeef">00</p>
   <input id="timersecondsBeef" type="hidden" value="5417">
</li>

VIEW

time_since = AddCooking.objects.raw("select * from heatcook_addcooking where time_start > now() - interval '180 minutes'")

for item in time_since:
        list_cooking[item.foodfk.name] = (int((time_now - item.time_start).total_seconds()))

Solution:
Use inside the for loop

{% for key, value in list_cooking.items %}
   <script>Script here</script>
   <li class="list-group-item">
   <p>{{key}}</p>
   <br></br>
   <p>Seconds:</p><p id='counter{{key}}'>00</p>
   <input id="timerseconds{{key}}" type="hidden" value="{{value}}">
   </li>
    
2
  • Your JS isn't working because {{key}} is not defined outside the for-loop. You'll rather have to select elements using class names, rather than IDs. Commented Jun 25, 2020 at 4:57
  • Using the <script> inside the loop I could access {{key}} Thanks! Commented Jun 25, 2020 at 10:50

1 Answer 1

1

You have to also iterate over the JavaScript code in the template:

{% for key, value in list_cooking.items %}
     <li class="list-group-item">
     <p>{{key}}</p>
     <br></br>
     <p>Seconds:</p><p id='counter{{key}}'>00</p>
     <input id="timerseconds{{key}}" type="hidden" value="{{value}}">
     </li>
     <script type="text/javascript">
         window.onload = jQuery(function ($) {
            var duration = $("#timerseconds{{key}}").val();
            duration = 10800 - duration;
            display = $("#counter{{key}}");
            startTimer(duration, display);
        });
    </script>
{% endfor %}
Sign up to request clarification or add additional context in comments.

4 Comments

Still get null var duration = document.getElementById("#timerseconds{{key}}").value;
I updated it to use JQuery instead. see if that works.
If that still doesn't work, please send the source of the rendered HTML page to ensure your key and value are rendering properly. Also post how you are creating the cooking_list object.
The JQuery is working. With the script inside the loop i could get {{key}} Thanks Michael!

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.