0

I have list input checkbox

<input type="checkbox" name="id_image" value="homies_01" class="id_image" />
<input type="checkbox" name="id_image" value="homies_02" class="id_image" />
<input type="checkbox" name="id_image" value="homies_03" class="id_image" />

<input type="text" name="id_image" value="" class="id_image_check" />

How to check multiple input checkbox i have show value to input type="text"as

<form>
<input type="text" name="id_image" value="homies_01, homies_02, homies_03" class="id_image_check" />
</form>
Harsh Patel
  • 6,334
  • 10
  • 40
  • 73
DinhS
  • 31
  • 9

5 Answers5

1

With jquery

$(".id_image").on("click",function(){
  $(".id_image_check").val(
    $(".id_image :checked").map((i,el) => el.value).join(", ")
  )
});
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • https://jsfiddle.net/gerhcm9c/. @Jons i have checkbox but don't value on input? any for help me? – DinhS Sep 16 '17 at 11:20
1

There is a perfecly solution with Vanilla Javascript

function attachListeners() {
  var checkboxes = document.getElementsByClassName('id_image')

  for (var i = 0; i < checkboxes.length; i++) {
      checkboxes[i].addEventListener('click', getAndPutValues, false);
  }
}

function getAndPutValues() {
  var result = [];
  document.querySelectorAll(".id_image:checked").forEach(function(item) { result.push(item.value);});

  document.querySelector('.id_image_check').value = result.join(', ');
}

attachListeners();
0

First of all you should use array notation for checkbox names

<input type="checkbox" name="id_image[]" value="homies_01" class="id_image" />
<input type="checkbox" name="id_image[]" value="homies_02" class="id_image" />
<input type="checkbox" name="id_image[]" value="homies_03" class="id_image" />

It will help you to post multiple values simultaneously

After that for checking values in jQuery you can use

<script type="text/javascript">
   $('[name="id_image[]"]').click(function(){
        var value = '';
        $('[name="id_image[]"]:checked').each(function(){
           value += $(this).val();
        });

       $('#id_image_check').val(value);
   });
</script>
hanish singla
  • 782
  • 8
  • 21
  • https://jsfiddle.net/7sndfram/1/ @hanish i have checkbox but don't value on input? any for help me? – DinhS Sep 16 '17 at 11:22
0

Try this :

<input type="checkbox" name="id_image" value="homies_01" class="id_image" onchange="toggleCheckbox(this)" />
<input type="checkbox" name="id_image" value="homies_02" class="id_image" onchange="toggleCheckbox(this)" />
<input type="checkbox" name="id_image" value="homies_03" class="id_image" onchange="toggleCheckbox(this)" />

<input type="text" name="id_image" id="id_image" value="" class="id_image_check" />

<script type="text/javascript">
    var arrImages = [];
    function toggleCheckbox(element) {
        console.log('element', element.value);

        arrImages.push(element.value);
        document.getElementById("id_image").value = arrImages;
    }
</script>
Chandru
  • 10,864
  • 6
  • 38
  • 53
-1

Just use square brackets [] at end of name if you want to submit it using form or use the following method in jQuery to get values of multiple selected checkboxes.

$('#show').click(function(){
  values = "";
  $('input.id_image:checked').each(function(){
    values += $(this).val() + " ";
  });
  if(values){
    $('#values').html(values);
  }else{
    $('#values').html("None");
  }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="id_image[]" value="homies_01" class="id_image" /> Homies 1
<input type="checkbox" name="id_image[]" value="homies_02" class="id_image" /> Homies 2
<input type="checkbox" name="id_image[]" value="homies_03" class="id_image" /> Homies 3

<br>
<button id="show">Show</button>

<br>
<div id="values"></div>
Deepansh Sachdeva
  • 1,168
  • 9
  • 16