Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Array results listed one below each other?

I have to mention from the beginning that i am new in this world, so any help would be more than appreciated!

I have some Bootstrap checkboxes that i want to print (window.print) using jQuery.

The only problem is that the array that i create from the checkboxes, lists the results one after another.

HTML:

                  <div class="form-check">
                <input class="form-check-input" name="type" type="checkbox" value="Question1" id="flexCheckDefault">
                <label class="form-check-label" for="flexCheckDefault">
                  Question1
                </label>
              </div>
              <div class="form-check">
                <input class="form-check-input" name="type" type="checkbox" value="Question2" id="flexCheckDefault">
                <label class="form-check-label" for="flexCheckDefault">
                  Question2
                </label>
              </div>

jQuery:

$("#results_message").text("The results are:");
$("button").on("click", function () {
 var array = [];
 $("input:checkbox[name=type]:checked").each(function () {
   array.push($(this).val());
 });
 $("#results_after").text(array);
 $('#results_after').printThis();
});

CSS:

#results_after {
  color: green;
  font-size: 24px;
  font-weight: bold;
  visibility: hidden;
}

#results_message {
  visibility: hidden;
}

@media print {
  #results_message,
  #results_after {
    visibility: visible;
  }
}

Because it's an array, the results are separated by a comma and i want them to be one below each other. I was thinking <br> would solve the situation but i dont know how to add it.

Many thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The short answer is: use array.join() to separate the array by a delimiter. I chose <br /> which meant I needed to use .html() rather than .text().

$("#results_after").html(array.join('<br />')).show()

The .show() is because you were hiding the output divs in the css.

$("#results_message").text("The results are:").show();
$("button").on("click", function() {
  var array = [];
  $("input:checkbox[name=type]:checked").each(function() {
    array.push($(this).val());
  });
  $("#results_after").html(array.join('<br />')).show();
});
#results_after {
  color: green;
  font-size: 24px;
  font-weight: bold;
  display: none;
}

#results_message {
  display: none;
}

@media print {
  #results_message,
  #results_after {
    visibility: visible;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-check">
  <input class="form-check-input" name="type" type="checkbox" value="Question1" id="flexCheckDefault">
  <label class="form-check-label" for="flexCheckDefault">
                  Question1
                </label>
</div>
<div class="form-check">
  <input class="form-check-input" name="type" type="checkbox" value="Question2" id="flexCheckDefault">
  <label class="form-check-label" for="flexCheckDefault">
                  Question2
                </label>
</div>
<hr>
<button>click</button>
<div id='results_message'></div>
<div id='results_after'></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!