I have following markup which is generated dynamically using C# in my asp.net MVC2 application. There could be more rows depending on data in database. I have shown two rows. one is view row other is edit row. by default I want ‘view’ row(s) visible and rows with id ‘edit’ will be invisible. I want to use jQuery so that:
- on click of toggle link, I want view row invisible and edit row visible
- on click of update/cancel images, I want edit row invisible and view rows visible. edit button will cause postback
- There can be more than one rows with same id (view or edit), do i need to use class instead of id?
<table>
<tr id="view">
<a id="toggle" class="icon-button" href="#">
</tr>
<tr id="edit" style="display:none">
<img id="update" alt="Update" src="/static/images/update.png">
<img id="cancel" alt="cancel" src="/static/images/cancel.png">
</tr>
<tr id="view">
<a id="toggle" class="icon-button" href="#">
</tr>
<tr id="edit" style="display:none">
<img id="update" alt="Update" src="/static/images/update.png">
<img id="cancel" alt="cancel" src="/static/images/cancel.png">
</tr>
</table>
[EDIT]
I used this but it is not working:
<script type="text/javascript">
$(function () {
$(".icon-button").click(function () {
alert('I am here');
$('.view,.edit').toggle();
return false;
});
$(".icon-button-cancel").click(function(){
alert('I am there');
$('.view,.edit').toggle();
return false;
}
});
Please suggest solution using jQuery/JavaScript or any any other ASP.NET alternative
So, assuming you make the following changes:
<td>cells to conform to a table format.(Some of these may just be becuase you posted a demo snippet, I’m not sure). Then, I made a couple of adjustments like made the cancel a link instead of an image, but an image will work.
Then the following will work (with jQuery):
DEMO