Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 2.02 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Delete button for each dynamically created row in a table without using onclick
  2. function testobject() {
  3.  //other stuff
  4.  
  5.  $stateadd.click(function() {
  6.   if ($stateselect.val() in states) { //already in array
  7.  
  8.   } else {
  9.    $temp = $("<tr><td nowrap>" + USStates[$stateselect.val()].Name + "</td><td><input type="button" class="testthing" value="Remove" /></td>").appendTo($selectedstates);
  10.    $temp2 = $temp.find('.testthing');
  11.  
  12.    $temp.delegate($temp2, 'click', function(e) {
  13.     //how to know what row to delete?  how to know which object the row is in?
  14.     alert($(e.target).attr('class')); //returns proper class, so I can get the button object itself
  15.    });
  16.  
  17.    states[$stateselect.val()] = $temp;
  18.   }
  19.  });
  20.  
  21.  //other stuff
  22. }
  23.        
  24. $('tableSelector').delegate('.testthing', 'click', function(e) {
  25.     //Here "this" will point to the remove button.
  26.  
  27.     var $tr = $(this).closest('tr');//this will give you the corresponding row  
  28. });
  29.        
  30. $('tableSelector').on('click', '.testthing', function(e) {
  31.     //Here "this" will point to the remove button.
  32.  
  33.     var $tr = $(this).closest('tr');//this will give you the corresponding row  
  34. });
  35.        
  36. $temp = $('<tr><td nowrap>' + USStates[$stateselect.val()].Name + '</td><td><input type="button" data-stateid="' + $stateselect.val() + '" class="testthing" value="Remove" /></td>').appendTo($selectedstates);
  37.        
  38. $('tableSelector').delegate('.testthing', 'click', function(e) {
  39.     //Here "this" will point to the remove button.
  40.  
  41.     var sateId = $(this).data('sateid');
  42.     var $tr = $(this).closest('tr');//this will give you the corresponding row  
  43.  
  44.     //Here you can access states[stateId] and do what you want
  45. });
  46.        
  47. slideUp('slow', function() {
  48.     // now that you have slided Up, let's remove it from the DOM
  49.     $(this).remove();
  50. });
  51.        
  52. $(document).on('click', '.testthing', function(e) {
  53.     alert($(e.target).attr('class'));
  54.  
  55.     var tr = $(this).closest('tr');
  56.     tr.slideUp('slow', function() {
  57.         // now that you have slided Up, let's remove it from the DOM
  58.         $(this).remove();
  59.     });
  60. });