0

I've been trying to bind a click event to a div element in an html file, that is part of a Django project. My menu.html is as follows-

{% extends "base.html" %}

{% block title %}Today's menu{% endblock %}

{% block head %}
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
        </script>

    <script type="text/javascript">
        $(document).ready(function() {
            $("#plus").bind("click", function () {
                $.get("/test/"+this.id+"/", function(data) {
                    if (data.fact_type=="T") {
                         item = data.fact_note;
                    }
                    $('#result')[0].innerHTML=item;
                });
            });
        });
        </script>

{% endblock %}

{% block content %}

{% for id,image,menu in imageList %}
<div style = "display:inline-block">
    <img src="{{ MEDIA_URL }}{{ image }}">
    <p>{{ menu }}</p>
    <div id="plus"><span>+</span></div>
    <div id="result">Click to add.</div>
</div>

{% endfor %}
{% endblock %}   

This template has been extended from the base.html template file, which is-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>{% block title %}{% endblock %}</title>
    {% block head %}{% endblock %}
</head>
<body>
    <h1>Welcome</h1>
    {% block content %}{% endblock %}
    {% block footer %}
    <hr>
    <p style="display:block>Footer.</p>
    {% endblock %}
</body>
</html>

Here's my views.py function -

def order(request, product_id):
    message = {"fact_type": "", "fact_note": ""}
    if request.is_ajax():
        fact = get_object_or_404(Fact, id=fact_id)
        message['fact_type'] = fact.type
        message['fact_note'] = fact.note
    else:
        message = "Error."
    return HttpResponse(simplejson.dumps(message), content_type='application/json')

But the click event doesn't bind to the div with the id plus. I've tried changing bind() to on() but with no luck.

Thinking this was caused because the document was probably not being ready, I also tried $(document).on('click','#plus',function () { }); , yet it didn't do anything.

1 Answer 1

3

The issue, is, it is absolutely essential that the IDs on a document are unique. Use a class in conjunction with a unique ID, and your code should work.

Example:

{% for id,image,menu in imageList %}

    <div style = "display:inline-block">
        <img src="{{ MEDIA_URL }}{{ image }}">
        <p>{{ menu }}</p>
        <div id="plus-{{id}}" class="plus"><span>+</span></div>
        <div id="result-{{id}}" class="result">Click to add.</div>
    </div>
{% endfor %}

And your script would look like this:

<script type="text/javascript">
    $(document).ready(function() {
        $(".plus").on("click", function () {
            var id = $(this).attr('id').split('-')[1]; // gives you the id
            $.get("/test/"+ id +"/", function(data) {
                if (data.fact_type=="T") {
                     item = data.fact_note;
                }
                $('#result-' + id).html(item); 
            });
        });
    });
    </script>
Sign up to request clarification or add additional context in comments.

6 Comments

Nope. It is still unclickable. I also tried to hard code the value of the id in the div, but it didn't work either. If the id has to be unique, then shouldn't we also get unique ids to the $(".plus").on("click", function () line as well?
first off, this was a general idea - next, you dont need the span inside the div now. Also, give a little padding around the plus, so there is more space for the click.
Did that. No change. I even put that div element outside the for loop and gave it the id '.plus'. Still. no. change.
Also, "result-{{id}}" is a string. So it won't parse the {{id}}. How do I get the {{id}} to be parsed?
are you sure {{id}} does not get resolved ? It should. Can you paste the resulting HTML in a fiddle, and paste the link here?
|

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.