Ch Hong Ch Hong - 29 days ago 13
jQuery Question

jquery remove selected value from textbox

<input class="y" id="imploding_feature3" type="hidden" name="product_labels_module[3][manual_products]" value=",,,,62,108,,45,">


<input class="y" id="imploding_feature4" type="hidden" name="product_labels_module[4][manual_products]" value=",,,,45,78,,26,">


I would like to get the value inputted from the user and then delete the value. For example, user enter value 45 then it will remove all 45 in textbox but other value will remind ,how can i do it using jquery??

Answer

check the output of this demo, I think it does what you want.

Updated script:

$('#removeProduct').on('click', function() {
	var input = $('#productId').val();
    
    // loop through each imploding_feature and remove value if exist
    $('input[id^="imploding_feature"]').each(function(ndx, elem) {
    	var values = $(elem).val();
        console.log('before =', values);
        var valuesarray = values.split(',').map(function(val, ndx) {
        	return (val === input)?'':val;
    	}).join(',');
        $(elem).val(valuesarray);
        console.log('after =', valuesarray);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="productId" value="">
<button id="removeProduct">
remove product id
</button>
<input class="y" id="imploding_feature3" type="hidden" name="product_labels_module[3][manual_products]" value=",,,,62,108,,45,">


<input class="y" id="imploding_feature4" type="hidden" name="product_labels_module[4][manual_products]" value=",,,,45,78,,26,">