I have an div field that when clicked in, creates a drop down menu. Whenever you click on the drop down option choices, the name will go to the div field #proposal-type
. You can keep clicking to add additional option choices.
My issue is, when I click on the x to remove the choice, I cannot figure out how to get the entire drop-item
that was just click on to be removed and fill back into the drop down list. Right now only the x is going back into the option list.
Does anyone have any ideas?
Also, could someone please share a quick pointer of how to get the option box list to go away when I click outside of it? It just remains there no matter what right now.
$('#proposal-type').click(function() {
$('#proposal-type-drop').addClass('active');
});
$('#proposal-type').text("Type of Project *");
$('.drop-item-input').on('change', function() {
var proposalVal = "";
var proposalHtml = "";
$('.drop-item-input').each(function() {
if ($(this).is(":checked")) {
proposalVal += $(this).val();
proposalHtml += '<div class="drop-item-selected"><span class="drop-item-close"></span>' + $(this).val() + '</div>';
$(this).closest('label').fadeOut();
};
//else if ($(this).is(":checked") === false) {
// $(this).closest('label').fadeIn();
//};
$('#proposal-type').val(proposalVal).html(proposalHtml);
$('#proposal-type-drop').removeClass('active');
});
//values
var type = $('.drop-item-input:checked').map(function() {
return $(this).val();
}).get().join(', ');
console.log(type);
});
$(document).on('click', '.drop-item-close', function() {
console.log("Triggered");
$(this).fadeOut("medium", function() {
$(this).detach().appendTo('#proposal-type-drop').fadeIn();
});
$(this).is(":checked") === false;
$(this).closest('label').fadeIn();
$(this).closest('.drop-item-selected').fadeOut();
});
.panel-input {
box-sizing: border-box;
border: 1px solid #858585;
display: inline-block;
vertical-align: top;
width: 45%;
margin: 1.5% 2% 2.5% 2%;
}
.proposal-input {
width: 100%;
}
#proposal-type-drop {
width: 45%;
display: none;
position: absolute;
}
#proposal-type-drop.active {
background: rgba(0, 0, 0, 1);
display: block;
height: auto;
z-index: 1;
}
.drop-item {
color: #FFF;
font-size: 1rem;
padding: 7px 5px;
font-family: 'Open Sans', sans-serif;
background: rgba(0, 0, 0, .8);
display: block;
width: 100%;
cursor: pointer;
}
.drop-item-input {
display: none;
}
.drop-item-selected {
background: #0085A1;
color: #333333;
padding: 5px;
font-size: .9rem;
font-weight: bold;
font-family: 'Roboto', sans-serif;
width: auto;
display: inline-block;
margin: 0 3px;
}
.drop-item-close {
display: inline-block;
background-image: url("http://www.clipartbest.com/cliparts/dT8/xnA/dT8xnAqAc.png");
background-size: 10px 10px;
background-repeat: no-repeat;
height: 10px;
width: 10px;
margin-right: 10px;
cursor: pointer;
transition: ease 0.1s;
-webkit-transition: ease 0.1s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="panel-input">
<div id="proposal-type" class="proposal-input"></div>
<div id="proposal-type-drop">
<label class="drop-item">Apple<input type="checkbox" name="prop-type[]" class="drop-item-input" value="Apple"></label>
<label class="drop-item">Banana<input type="checkbox" name="prop-type[]" class="drop-item-input" value="Banana"></label>
<label class="drop-item">Cake<input type="checkbox" name="prop-type[]" class="drop-item-input" value="Cake"></label>
</div>
</div>