views:

49

answers:

2

Hey there.

This may already have an answer here but I can't seem to find the right search string so..

I have, almost the same as the comments in SO, a page that has many instances of the same PartialView.

The partial view has a button in it that when pressed adds a comment associated to the product the PartialView represents.

I can easily make this work if I do a full post back and all the comments are updated but it's an ugly solution.

What I need is on the button that was pressed I need a Json call to the server. I then save the comment and pass back a new PartialView with a list of new comments.

I want to then only update the comments on the particular product that I added the comment to.

I use this in the javascript to assign to the button;

$(".clsTest").unbind("click").click(function(evt) {

But it only attaches this to the first instance of the button it finds not all.

I feel I'm missing some key element of knowledge here. I'm not even sure I'm asking this question correctly.

A: 

I'm no jQuery ninja but I think you need to look at the jQuery '.each' method.

Maybe something like this(?):

$(".clsTest").each(
    function() {
        $(this).unbind("click").click(function(evt) {
            //do your stuff...
        });
    }
);

HTHs
Charles

Charlino
You don't need to do that. Unbind works on all matched elements. Check the docs: http://docs.jquery.com/Events/unbind
RaYell
Yeah, that was just copy and paste code from the question.
Charlino
+2  A: 

Why don't you use jQuery live for that. You won't have to add event handler whenever you add a new element matching the selector because jQuery will do it for you.

$('.clsTest').live('click', function () {
    ...
});
RaYell
+1 for trying but I think I described the problem incorectly. Charlino got me a little closer but have created another question.
griegs