JavaScript checkbox pass to input field has conflict in jquery pass input checkbox value to controller

I'm trying to add the value of the checkbox in the field the is closest to it. But I think I missed something. Please see my code below:

$(document).ready(function(){
    $("input:checkbox").click(function() {
        var output = "";
        $("input:checked").each(function() {
            output += $(this).val() + ",";
        });
        
        $(this).closest('.divs').find(".result").val(output.trim());
        
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="divs">
  <input type="text" class="result"/><br>

  <input type="checkbox" value="1">1<br>
  <input type="checkbox" value="2">2<br>
  <input type="checkbox" value="3">3
</div>

<br><br>

<div class="divs">
  <input type="text" class="result"/><br>
  <input type="checkbox" value="1">1<br>
  <input type="checkbox" value="2">2<br>
  <input type="checkbox" value="3">3
</div>
Answer:1

change the input type text attr with classname instead of id. Target the checkbox value via parent div

Updated 1

with , unsing map() function

Upadted 2

Added with empty value

$(document).ready(function(){
    $("input:checkbox").change(function() {
        var output  =$(this).closest('.divs').find("input:checked").map(function() {
           return $(this).val();
        }).get();
        var res = output.length ==0 ? 'empty' : output.join(',');
        $(this).closest('.divs').find(".field_results").val(res);
        
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="divs">
  <input type="text" class="field_results" placeholder="empty"><br>

  <input type="checkbox" value="1">1<br>
  <input type="checkbox" value="2">2<br>
  <input type="checkbox" value="3">3
</div>

<br><br>

<div class="divs">
  <input type="text" class="field_results" placeholder="empty"/><br>
  <input type="checkbox" value="1">1<br>
  <input type="checkbox" value="2">2<br>
  <input type="checkbox" value="3">3
</div>
Answer:2

I am using a <asp:FileUpload> to upload a PDF file to my web page. But after clicking on browse the window opens, and once I select a file and click on Open i want to get the file name and ...

I am using a <asp:FileUpload> to upload a PDF file to my web page. But after clicking on browse the window opens, and once I select a file and click on Open i want to get the file name and ...

  1. file name from path
  2. file name from path python
  3. file name from path c#
  4. file name from file descriptor
  5. file name from user input java
  6. file name from path java
  7. file name from folder to excel
  8. file name from url
  9. file name from uri android
  10. file name from path bash
  11. file name from path php
  12. file name from url php
  13. file name from path vba
  14. file name from url swift
  15. file name from path ruby
  16. file name from trigger
  17. file name from path shell
  18. filename from file path
  19. file name from url python
  20. file name from file object python

I am trying to test a method using Jest... The method should return Promise.reject() . Here is the code I wrote: test('testing Invalid Response Type', () => { const client = new ...

I am trying to test a method using Jest... The method should return Promise.reject() . Here is the code I wrote: test('testing Invalid Response Type', () => { const client = new ...

What I would like to do is calculate the value of a coordinate in the pascal triangle, I got a table with X and Y axis where I have my pascal triangle aligned at the top and the left, what I'm trying ...

What I would like to do is calculate the value of a coordinate in the pascal triangle, I got a table with X and Y axis where I have my pascal triangle aligned at the top and the left, what I'm trying ...

I've given some thoughts on declaring a variable called undefined and evaluating a truthy if-else expression with it. I found out that it is not possible in global scope. But in MDN I've found some ...

I've given some thoughts on declaring a variable called undefined and evaluating a truthy if-else expression with it. I found out that it is not possible in global scope. But in MDN I've found some ...