lundi 18 septembre 2017

checkbox pass to input field has conflict in jquery

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("#field_results").val(output.trim());
        
    });
});
<script src="http://ift.tt/1oMJErh"></script>
<div class="divs">
  <input type="text" id="field_results"/><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" id="field_results"/><br>
  <input type="checkbox" value="1">1<br>
  <input type="checkbox" value="2">2<br>
  <input type="checkbox" value="3">3
</div>



Aucun commentaire:

Enregistrer un commentaire