Daniel Daniel - 4 months ago 31
jQuery Question

Add/remove textbox value when checkbox change

I have the following jquery code where i want to remove the value from the textfield if the checkbox is unchecked and add if it is checked.

initially all the values comes in the textbox as the

seperator. and all checkboxes checked

here is y piece of code:

$(document).on('change','._invoice',function() {
var mystr = $(this).attr('data-id').is(":checked");
if(mystr) {
var returnVal = confirm("Are you sure?");
$(this).attr("checked", returnVal);

Text field values and i also want to remove the separator and add the name at the last with
as separator.

Robert Berenson^Nancy Foster^Richard Gourhan^LORI HEDMAN^Pui Hoang^Linda Lee^Kristen McDonald^Matthew Miller^Tricia Roland^Terry West


A simple way is that change text of textbox on checkbox change event. Then means you need to get text of every checked checkbox and set it to textbox, when any checkbox is changed.

    var text = "";
        text += text != "" ? "^" : "";
        if ($(this).prop("checked"))
            text += $(this).val();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="check" value="Text1" />
<input type="checkbox" class="check" value="Text2" />
<input type="checkbox" class="check" value="Text3" />
<input type="checkbox" class="check" value="Text4" />
<input type="checkbox" class="check" value="Text5" />
<input type="text" class="text" />