K.Buxton
K.Buxton

Reputation: 1

Can't get javascript to work on my website; Django Polls

For an assessment at UNI i need to make a webpage list the contents of an object dynamically. Sorry I am very new to this and need to complete this assessment.

The code below is what I need to change to get the website to display the contents of an object below the link when it is clicked, as of now, when i click the link nothing happens.

I have probably explained this very wrong. The assessment is based on the Django webpage tutorials.

Please ask me to explain further if required, as I don't really understand how to explain my issue. The attached image is what the outcome needs to be..

    {%load staticfiles%}

<script src="(% static "polls/jquery-3.3.1.js" %)"></script>


{% if latest_question_list %}
    <ul id="question-list">
    {% for question in latest_question_list %}
        <li id=" {{ question.id }}"><a href="#">{{ question.question_text }}</a></li>
        <ul style="display: none">
            <li></li>
        </ul>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}


<script>
$(document).ready(function()    (
    $("#question-list li").click(fuction(){

        var question_id = string($(this).attr('id'));
        var detail.id = "#detail-" + question_id;

        if ($(detail_id).is("visible"))    {
        $(detail_id).hide();
        }
        else {

            var url = "/polls/" + question_id + "/";

            $.getJSON(url, function(data)    {

                $.each(data, function(i, details) {
                    $(detail_id + " li").text(details);
                    $(detail_id).show();

                });
            });
        }
    });
});

</script>

Image that the response needs to look like

Upvotes: 0

Views: 39

Answers (1)

Abhijeet Pal
Abhijeet Pal

Reputation: 468

Nothing is happening because when you see the (a) tag href which is gives the address of the question is set to #.

Which means nothing will happen even when you click it.

Just change the href to the corresponding element tag. I need to see the views to tell the exact tag it would be something like question.tag

Update -

Replace this <a href="#">' by '<a href="{{ question.detail }}">

Upvotes: 1

Related Questions