1

When I click the span on html I cant make variable span id with id="mylink-${i}" I couldn't get this with var mylink = document.getElementById("mylink-"+id) in javascript part.

and also at onclick="theFunction(i)" console gives me i is not defined error but it should be defined because of {% for i in data %} i is one of the elements of data how can ı solve this

    <div class="row sideBar">
        {% for i in data %}

            <div class="row sideBar-body">
                
                <div class="col-sm-9 col-xs-9 sideBar-main">
                <div class="row">

                    <a   href= "{% url "start_chat" i.room %}" onclick="theFunction(i)" >
                    
                    <div class="col-sm-8 col-xs-8 sideBar-name">
                    
                    <span class="name-meta"  id="mylink-${i}"> {{i.name}} 
                    
                    </span>
                    
                    </div>
                </a>
                    
                </div>
                </div>
            </div>

        {% endfor %}

    </div>

                           

const rooms = JSON.parse(document.getElementById('data').textContent)


function theFunction(id) {

       var mylink = document.getElementById("mylink-"+id)

}


                                       
  • 1
    I'm not familiar with django, but for `theFunction(i)`, you're not indicating it as a django variable. It's being written as a string, then interpreted as an undefined variable in JS. – mykaf Aug 31 '22 at 14:53
  • 1
    For js issues, always **check the rendered HTML** - you'll see that `onclick="theFunction(i)"` is exactly that as the `i` is part of the string literal and not the `i` variable. – freedomn-m Aug 31 '22 at 14:55
  • "*...in javascript part.*" - please include the javascript part. Specifically `document.getElementById("mylink-"+id)` - what's `id` here? – freedomn-m Aug 31 '22 at 14:56
  • @mykaf yes I know I was wondering how can I pass a variable – Batu Solmaz Aug 31 '22 at 14:57
  • If you know what the problem is, then ask how to solve it. At the moment, your question indicates that you do not know the cause. – freedomn-m Aug 31 '22 at 14:58
  • Slightly confusing as the variable name `i` is normally used as an integer *i*increment / *i*ndex - eg `for (var i=0;i<10;++i)` or `foreach (var i in myarray) { myval=myarray[i]`. But looks like `i` in your case is a class as you have `i.room`, so even if you could output the variable `i` it would not make sense as it would not be a plain `id`/integer (from reading your code) – freedomn-m Aug 31 '22 at 15:11
  • 1
    Again, not familiar with django, but you seem to be using `{{i.name}} ` later on, so can you use curly braces to pass the `i` to `theFunction()`? – mykaf Aug 31 '22 at 15:11
  • 1
    @mykat they also seem to be using `${i}` and `{% url "start_chat" i.room %}` - I guess *one* of those must work :) – freedomn-m Aug 31 '22 at 15:12
  • 1
    Does this answer your question: [output the value of a variable in Django](https://stackoverflow.com/questions/12848286/print-the-value-of-a-variable-in-python-django) – freedomn-m Aug 31 '22 at 15:14

0 Answers0