Created
January 22, 2014 07:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 å 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