- Delete button for each dynamically created row in a table without using onclick
- function testobject() {
- //other stuff
- $stateadd.click(function() {
- if ($stateselect.val() in states) { //already in array
- } else {
- $temp = $("<tr><td nowrap>" + USStates[$stateselect.val()].Name + "</td><td><input type="button" class="testthing" value="Remove" /></td>").appendTo($selectedstates);
- $temp2 = $temp.find('.testthing');
- $temp.delegate($temp2, 'click', function(e) {
- //how to know what row to delete? how to know which object the row is in?
- alert($(e.target).attr('class')); //returns proper class, so I can get the button object itself
- });
- states[$stateselect.val()] = $temp;
- }
- });
- //other stuff
- }
- $('tableSelector').delegate('.testthing', 'click', function(e) {
- //Here "this" will point to the remove button.
- var $tr = $(this).closest('tr');//this will give you the corresponding row
- });
- $('tableSelector').on('click', '.testthing', function(e) {
- //Here "this" will point to the remove button.
- var $tr = $(this).closest('tr');//this will give you the corresponding row
- });
- $temp = $('<tr><td nowrap>' + USStates[$stateselect.val()].Name + '</td><td><input type="button" data-stateid="' + $stateselect.val() + '" class="testthing" value="Remove" /></td>').appendTo($selectedstates);
- $('tableSelector').delegate('.testthing', 'click', function(e) {
- //Here "this" will point to the remove button.
- var sateId = $(this).data('sateid');
- var $tr = $(this).closest('tr');//this will give you the corresponding row
- //Here you can access states[stateId] and do what you want
- });
- slideUp('slow', function() {
- // now that you have slided Up, let's remove it from the DOM
- $(this).remove();
- });
- $(document).on('click', '.testthing', function(e) {
- alert($(e.target).attr('class'));
- var tr = $(this).closest('tr');
- tr.slideUp('slow', function() {
- // now that you have slided Up, let's remove it from the DOM
- $(this).remove();
- });
- });