Skip to content

Instantly share code, notes, and snippets.

@jsven69gist
Created January 22, 2014 07:40
Show Gist options
  • Save jsven69gist/8554881 to your computer and use it in GitHub Desktop.
Save jsven69gist/8554881 to your computer and use it in GitHub Desktop.
jQuery: Select with add to table with "self-delete" button in table row
function delpart(id) {
$(id).closest('tr').remove();
}
$("#AddPart").click(function() {
'use strict';
var userID = $('#sel_Participants').val(); // Get value from select
var userName = $('#sel_Participants option:selected').text(); // Get text from select
if (userID > 0) { // UserID
var idToFind = '#h_UserID_P_' + userID;
var found = $(idToFind).val();
if (found == undefined) { // Means not found - we don't want same name twice in list, do we? ;-)
var btnID = 'b_DelPart_' + userID;
var new_row = '<tr style="background-color: transparent;"><td style="width: 20px; text-align: left; border-right: none; padding: 1px 2px;"><button type="button" id="' + btnID + '" class="delpart" style="background: transparent url(../img/cancel16.png) no-repeat center center; border: none; color: transparent; width: 16px; height: 16px;" title="Klikk for &aring; fjerne deltaker">-</button></td><td style="text-align: left; padding: 1px 2px; border-left: none;">' + userName + '<input type="hidden" value="' + userID + '" name="UserID_P[]" id="h_UserID_P_' + userID + '" /></td></tr>';
$('#Participants').append(new_row); // Add to table with id Participants
btnID = '#' + btnID;
$(btnID).click(function() {
delpart(this);
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment