0

I am basically new in using Javascript and Django.

This is my script:

<script>
        $(document).ready(function() {
            $("#source").change(function() {
                var el = $(this);

                var reg = [];
                var name = [];

                {% for item in city %}

                    reg.push({{ item.reg }});
                    name.push({{ item.name }});

                {% endfor %}

                var a = getElementById("status").length;

                for(val i = 0; i<a; i++){
                    if(el.val() == reg[i]){
                        $("#status").append("<option id = "+ reg[i] +">" + name[i] + "</option>");
                    }

                }
            });
        });
    </script>

This is my form:

<form method = "POST">
        {% csrf_token %}
        <select id="source" name="source">
            <option>-----</option>
            {% for item in region %}
                <option id = {{ item.id }}>{{ item.name }}</option>
            {% endfor %}
        </select>

        <select id="status" name="status">
            <option>-----</option>
        </select>

        <select id="3">

            <option>-----</option>
            {% for item in zip %}
                <option id = {{ item.cit }}>{{ item.num }}</option>
            {% endfor %}

        </select>
    </form>

What I want to achieve is that the second dropdown box will display the contents under the selected item in the first dropdown box. I actually don't understand what am I doing wrong because it doesn't show any result whenever I load it, and there are no errors displayed too. What am I missing? Thanks in advance!

2
  • The django template tags are already finished rendering when the page finishes loading, so when your javascript starts to execute, there's no for loop anymore. Commented Jan 8, 2016 at 19:45
  • 1
    If you're new to Javascript I would personally suggest gaining a level of comfortability with the language before using any Frameworks or APIs Commented Jan 8, 2016 at 19:45

1 Answer 1

1

I suspect this part:

            {% for item in city %}

                reg.push({{ item.reg }});
                name.push({{ item.name }});

            {% endfor %}

Unless {{ item.name }} evaluates to quoted values like 'cardiff', it'll render like:

name.push(cardiff)

And that would be undefined. Maybe with some quotes:

name.push('{{ item.name }}')

Also, shouldn't the for loop be like this ?

for(val i = 0; i<reg.length; i++){

There is also a typo in {{ item.cit }}.

You should also inspect and paste the generated HTML and JS.

!!! Pro-tip: You should use the JS debugger from your browser !!!

Sign up to request clarification or add additional context in comments.

1 Comment

I tried that and it did not work but i found a solution. It is almost the same as your answer by doing it like this var reg = [{% for item in city %}"{{ item.reg }}"{% if not forloop.last %},{% endif %}{% endfor %}];

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.