+2  A: 

YOu could uses a CSS selector in your JS to get all buttons with a particular class assigned to them. Then you can wire up you click event to them. So your HTML would be something like this:

<% foreach (var m in Model.UserMessages) { %>
    <div class="um" id="m<%=Html.AttributeEncode(m.ID) %>">
        <p class="mh"><%= Html.Encode (String.Format ("From {0} ({1})", m.From, m.Sent)) %></p>
        <p><%= Html.Encode (m.Text) %></p>
        <% using (Html.BeginForm ("CloseMessage", "Home", new { id = m.ID })) { %>
            <input type="submit" class="MyCloseButton" value="Close<%= Html.AttributeEncode (m.ID) %>" id="c<%= Html.AttributeEncode (m.ID) %>"/>
        <% } %>
    </div>
<% } %>

I've only added the class="MyCloseButton" to your input

Then in your JS (I use mootools but JQuery has CSS selectors too but you will need to port it sorry) you can do something like this:

window.addEvent( "domready", function() {
  $$("input.MyCloseButton").each( function( button ) {
    // Add your code here, you can reference button.id etc for each of your buttons
  }
});

This way you only have to write out one little function in plan JS and not worry about doing it server-side, all you need to do it decorate your buttons with a css class so the JS can find them.

Pete Duncanson
Ahh if only my example had been in jQuery...!
Pete Duncanson
+1  A: 

I understand its not pretty, but its not that bad either. I think there are easier ways to do this, however.

You can use jquery's positional selectors from the button's click event handler. Then just assign the same handler to every button.

<script type="text/javascript">
    $(document).ready(function() {        
        $("input[type=submit]").click(function(){
            $(this).parent().slideup("slow");
            $.post("Home/CloseMessage/" + $(this).parent().attr("id"));
            event.preventDefault();
          });
        });
    });
</script>
Will
+3  A: 

You could just create one javascript function that takes the element IDs as parameters :

function myFunction(ID) {
    $.post("Home/CloseMessage/" + ID);
    $("#m" + ID).slideUp("slow");
}

And :

<% foreach (var m in Model.UserMessages) { %>
<div class="um" id="m<%=Html.AttributeEncode(m.ID) %>">
    <p class="mh"><%= Html.Encode (String.Format ("From {0} ({1})", m.From, m.Sent)) %></p>
    <p><%= Html.Encode (m.Text) %></p>
    <% using (Html.BeginForm ("CloseMessage", "Home", new { id = m.ID })) { %>
        <input type="submit" value="Close<%= Html.AttributeEncode (m.ID) %>" 
        id="c<%= Html.AttributeEncode (m.ID) %>" 
        onclick="myFunction('<%=Html.AttributeEncode(m.ID)%>')"/>
    <% } %>
</div>
<% } %>
çağdaş
It is indeed a nice and elegant solution. One problem though, it still posts to the server and doesnt prevent a screen refresh. Is there any way to prevent the form post?
Hemant
Oh, right. I forgot it was a submit button. I usually make the input element's type "button" instead of "submit". But you could also try returning false from your function, that too should give you the behaviour you want.
çağdaş
Works as a charm! Thanks :)
Hemant
You're welcome :)
çağdaş