4

$('input[name="accounts"]').on('click', function() {
  $('input[name="accounts"]:checked').each(function() {
    if (index == 0) {
      txt = $(this).val();
      $("#accounts-text").html(txt);
    } else {
      $("#accounts-text").append(txt);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label><b>Accounts</b></label><br>
<input type="checkbox" name="accounts" value="Gmail"> Gmail
<br>
<input type="checkbox" name="accounts" value="Facebook"> Facebook
<br>
<input type="checkbox" name="accounts" value="Instagram"> Instagram
<br>
<div class="form-group">
  <label>Accounts: </label>
  <h5 id="accounts-text"></h5>
</div>

In the above code,help me to find out the error while trying to display the check box values on select in the same page.

2
  • Do you want to display all checked values on #accounts-text? Commented Oct 25, 2018 at 3:50
  • Yes, I want to display all the checked values Commented Oct 25, 2018 at 3:52

2 Answers 2

6

You can use map() and get() to get all values. This will return the array of all checked values. Use join() to convert the array into string.

$('input[name="accounts"]').on('click', function() {
  var result = $('input[name="accounts"]:checked').map(function() {
    return this.value;
  }).get();

  $("#accounts-text").html(result.join("<br />"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label><b>Accounts</b></label><br>
<input type="checkbox" name="accounts" value="Gmail"> Gmail
<br>
<input type="checkbox" name="accounts" value="Facebook"> Facebook
<br>
<input type="checkbox" name="accounts" value="Instagram"> Instagram
<br>
<div class="form-group">
  <label>Accounts: </label>
  <h5 id="accounts-text"></h5>
</div>

Sign up to request clarification or add additional context in comments.

5 Comments

Can you please explain what map() actually does here and why we have to convert array into string ?
map() loop thru the jQuery object and translates all items. In this case, it returns the value.
The get() part is converts it to an array.
Thankful for your explanation.
Happy to help :)
1

try this

$('input[name="accounts"]').on('click', function() {
          var txt  = [];
            $.each($("input[name='accounts']:checked"), function(){            
                txt.push($(this).val());
            });
            //alert("out puts are: " + txt.join(", "));
            $("#accounts-text").html(txt.join("<br />"));
});

  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label><b>Accounts</b></label><br>
<input type="checkbox" name="accounts" value="Gmail"> Gmail
<br>
<input type="checkbox" name="accounts" value="Facebook"> Facebook
<br>
<input type="checkbox" name="accounts" value="Instagram"> Instagram
<br>
<div class="form-group">
  <label>Accounts: </label>
  <h5 id="accounts-text"></h5>
</div>

1 Comment

What exactly the function push() does here ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.