Guest User

Untitled

a guest
Apr 21st, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. // This is a dynamically generated page with multiple forms where each form contains a button which is
  2. // either an add or delete button and toggles on click. The form actions gracefully degrade. The original
  3. // button always works with ajax, but the second doesn't rebind. I tried using the event delegation pattern
  4. // and the copy events to no avail. The fact that the whole form needs to get replaced seems to be the problem
  5. // since this isn't a simple append.
  6. //
  7. // Here is the HTML that we start with (for example):
  8. //<td id="td_14"> <form class="create" method="post" action="/cart_items" id="14"> <input type="hidden" name="id" value="14" />
  9. //<input type="hidden" name="to" value="/tags" />
  10. //<button type="submit" id="14" class="button positive"><img src="/stylesheets/lib/img/icons/tick.png" alt="" />Add</button>
  11. //</form>
  12. //</td>
  13. // or
  14. //
  15. //<td id="td_3"> <form class="delete" method="post" action="/cart_items/3" id="3"> <input type="hidden" name="to" value="/tags" />
  16. //<input type="hidden" name="_method" value="delete" />
  17. //<button type="submit" id="3" class="button remove"><img src="/stylesheets/lib/img/icons/tick.png" alt=""/>Remove</button>
  18. //</form>
  19. //</td>
  20.  
  21. // Here is the javascript using jquery
  22. $(document).ready(function(){
  23. $('.delete').click(function(event){
  24.  
  25. var id = $(this).attr("id")
  26. var $newForm = $('<form method="post" action="/cart_items" id="' + id + '" class="create"><input type="hidden" name="id" value="' + id + '" /><button id="' + id + '" type="submit" class="button positive"><img src="/stylesheets/lib/img/icons/tick.png" alt=""/> Add</button></form>');
  27. var $tgt = $(event.target);
  28. if ($tgt.is('button')) {
  29. $.ajax({
  30. type: "POST",
  31. data: {_method: "delete"},
  32. url: '/cart_items/' + id,
  33. dataType: "script",
  34. beforeSend: function(xhr){ confirm("Are you sure?");},
  35. success: function(data, status) {
  36. $tgt.parent().replaceWith($newForm);
  37. }
  38. });return false;
  39. }
  40. });
  41. $(".create").click(function(event){
  42.  
  43. var id = $(this).attr("id")
  44. var $newForm = $('<form method="post" action="/cart_items/' + id + '" id="' + id + '" class="delete"><input type="hidden" name="_method" value="delete" /><button type="submit" id="' + id + '" class="button remove"><img src="/stylesheets/lib/img/icons/tick.png" alt=""/> Remove</button></form>');
  45. var $tgt = $(event.target);
  46. if ($tgt.is('button')) {
  47. $.ajax({
  48. type: "POST",
  49. data: {id: id},
  50. url: "/cart_items",
  51. dataType: "script",
  52. success: function(data, status) {
  53. $tgt.parent().replaceWith($newForm);
  54. }
  55. });return false;
  56. }
  57. });
  58.  
  59. });
Add Comment
Please, Sign In to add comment