views:

32

answers:

2

I am building a similar wall feature to what facebook has, comments etc.

I am making use of django's comments framework and jquery to post and get on success, and I am looking at a way of extracting the the hidden id_object_pk's value and using that also as the forms id

Your normal form is rendered in the following way.

<form action="/comments/post/" method="post" class="comment-form" id="">
    <input type="hidden" name="content_type" value="wall.post" id="id_content_type" />
    <input type="hidden" name="object_pk" value="76" id="id_object_pk" />
    <input type="hidden" name="timestamp" value="1283848690" id="id_timestamp" />
    <input type="hidden" name="security_hash" value="ccf0e2f3cbbd57cb043df3f304a8dd50a74e972b" id="id_security_hash" />

How can I access those details?

A: 

My jQuery fu is very weak, so take this answer with a pinch of salt. There are probably better ways to do this.

First, you need to find the input with the comment id.

var element = $('#id_object_pk');

Next, extract the comment id itself. That would be the value attribute of the input element.

var comment_id = element.attr('value');

Finally update your form's id attribute and set it to comment_id.

var form = $('.comment-form');
form.attr('id', comment_id);
Manoj Govindan
yeah that makes sense, I'll try it out and see. Although I know you can remove the second line. and make the first var element = $('#id_object_pk').val();
ApPeL
that does work, however based on the fact that I have more than one form, all the form IDS end up being the same i.e. 77 or so.
ApPeL
That is because all your forms share the same style class. _If_ you use different, non-empty ID for each of your forms initially then you can use that ID to select and _replace_ as appropriate.
Manoj Govindan
A: 

Managed to sort this out by making use of the .each funtion in jQuery.

$('.comment-form').each(function(){
    var element = $(this).find('#id_object_pk').val();
    $(this).attr('id', element);
});
ApPeL