Is it possible to pass a Django id inside an HTML element, to a jQuery function?

Viewed 284

I am displaying a list of blog posts in a for loop, each has a comment form with send button underneath it. I need to let Django know which specific post has been clicked on so have added {{post.id}} to the button element's id. How do I then pass this information to jQuery? Is it possible to do something like this?

<button id="addComBtn{{post.id}}...>Send</button>

$('#addComBtn //post.id here').click(function() {
    ...
});

Or is there a better way?

1 Answers

Why haven't you tried out?

Yes it is possible. This should be the solution

<button id="addComBtn{{post.id}}...>Send</button>

$('#addComBtn{{ post.id }}').click(function() {
    ...
});

Because first Django renders the page, with its own engine(meaning, it doesn't check anything else outside of {%%}s and {{}}s), then it sends to the client, and when it renders, it will replace that.

Related