I'm trying to work on a 'Select Attendees' list for a website I'm working on and have got to a point where I'm getting stuck.
At the moment, what you can do is select options from the drop down list, which will show the name and populate an input field with these values (which I will be using later on to post to the database these values).
The problem I am having is that when deleting the name from the list, it is not deleting from the field value too.
Basically, when you delete one of the names, I want it to take that value out of the forms value and the appended ; to be removed too.
HTML
<select name="attendees" class="attendees">
<option value="" disabled="disabled" selected="selected">Please Select Attendees</option>
<option value="12" class="12">Name 1</option>
<option value="13" class="13">Name 2</option>
<option value="14" class="14">Name 3</option>
</select>
<div class="attendeesList" style="width: 370px;">
<input type="text" name="theAttendees" class="theAttendees" value="" style="width: 370px;" />
</div>
jQuery
(function($) {
$('.attendees').change(
function() {
var theSelect = $('.attendees option:selected');
getName = theSelect.text();
getClass = theSelect.val();
theAttendees = $('input.theAttendees').val();
if (theSelect.hasClass('noMore')) {
$('span.'+ getClass).animate({"color":"red"}, 100);
setTimeout(function() {$('span.'+ getClass).animate({"color":"#000"}, 500)}, 1000) ;
} else {
$('.attendeesList').append('<span class="'+ getClass +'">' + getName + '<a href="#" id="d" class="'+getClass+'"><img src="images/iconDelete.png" style="float:right"></a></span><br class="'+ getClass +'"/>');
theSelect.addClass('noMore');
$('input.theAttendees').val(theAttendees + ' ' + getName + ';');
}
});
}(jQuery));
(function($) {
$('div.attendeesList').on('click', 'a#d', function() {
var hrefSpan = $(this).parent();
classID = $(this).attr('class');
getName = $('.attendees option.'+ classID).text();
hrefSpan.remove();
$('br.'+ classID).remove();
if ($('.attendees option.'+ classID).hasClass('noMore')) {
$('.attendees option.'+ classID).removeClass('noMore');
}
});
}(jQuery));
I appreciate that it's probably not the easiest code to read and probably isn't as good as it can be, but I'm learning jQuery and trying to use it as much as possible and this is functional, so I can work on improving it once it works. If you feel like letting me know where my code can be optimised, I'd appreciate that too!
Thanks!