I have a form with a lot of options.
In the form, I can make the data private or public so;
- Make the data private : yes, no (radio buttons)
- If clicked yes, displaying a hidden div with 2 more form options; a. member name search (text input) b. selected members (multi selectbox).
If I click make the data private "yes", and search for a username, it is displaying found results like a suggestion box. When I click on a username (in one of the found results displaying in suggestion box), I can add it successfully to my multi selectbox (selected members)
If I decide to make it public and click make the data private "no" after adding some members to my multi selectbox (selected members), I'm deleting all options, and adding 1 option back in there (Public View).
The problem starts after here. Before submitting the form, if I click make the data private "yes" again, (1; clicked yes, selected some members, 2; clicked no and removed all options from selectbox, 3; clicked yes again), search for some members and click on them, it is adding 2 times in my multi selectbox (selected members).
Interestingly, if I do it 2 times (add some members, click no for privacy and delete options), next time when I want to add some members it is adding 2 times. If I do it 5 times, it is adding 5 times.
Here is my Jquery;
// Private Options
$('input[name=make_private]').bind('click', function() {
// Make It Private
if ($(this).val() == 1) {
$('.frm_make_private_1').fadeIn(1000);
// Get clicked member
$('a.member').live('click', function() {
var username = $(this).text();
var id = $(this).attr("id");
// Add it to the Allowed Members Multi Selectbox
$('#allowedMembers').append($("<option></option>").attr("value", id).text(username).attr("selected", "selected"));
// Don't load the page
return false;
});
}
// Make It Public
else {
// Hide everything about Product Privacy
$('.frm_make_private_1').fadeOut(1000);
// Remove All Options
$("#allowedMembers option").each(function() {
$(this).remove();
});
// Add Public Option
$('#allowedMembers').append($("<option></option>").attr("value", 0).text("Public View"));
}
});