Are the results displayed in a vertical array format?

Being new to this world, I would greatly appreciate any assistance!

I am working with Bootstrap checkboxes and trying to print them using jQuery's window.print function.

However, the issue I am facing is that the array I create from the checkboxes displays 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;
  }
}

As the results are displayed as an array with commas, I desire them to be listed vertically. I have considered using <br> tags to achieve this, but I'm unsure how to implement it.

Thank you in advance!

Answer №1

If you're looking for a quick solution, you can use the array.join() method to separate the array elements with a specified delimiter. In this case, I've used <br />, which required using .html() instead of .text().

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

The reason for adding .show() is because the output divs were initially hidden 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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble with innerHTML in a for loop when using getJSON

My current challenge involves displaying a series of JSON results within a div tag using innerHTML. <script> $(document).ready(function() { var html2 = ''; var thread_id = ''; var created_thread_ids ...

Encountering the error "java.lang.IndexOutOfBoundsException: Index: 1, Size: 1" while attempting to choose the second option in the dropdown menu

I need to choose the third option from a drop-down list that is enclosed in a div element. However, when I try to retrieve the items in the drop-down using Selenium, the size of the drop-down list is only showing as 1 even though there are actually 10 it ...

Attempting to configure Kafka consumer and producer components

Trying to establish a basic producer-consumer flow with Kafka, utilizing node-rdkafka Operating in debug: 'all' mode, the logs display: Producer: test [0]: MessageSet with 1 message(s) delivered Consumer: Fetch topic test [0] at offset 38 (v2) ...

How can I locate the following table after choosing an element using a jQuery selector?

I'm looking for a selector that can start at the element I click on and locate the next item with a specific class on the page. Here's an example to illustrate my request: <table> <tr> <td>Some text here</td> <td>&l ...

Obtaining HTML data with ajax within a WordPress post

Greetings! I've been putting together a website and I'm eager to include a unique timeline in each of my posts. I'm utilizing WordPress for this project. However, since the timeline I want to insert will vary from post to post, I am unable t ...

Display a division upon choosing an option

I am working on a project that involves a selection menu in the form of a drop-down list. <select> <option id="one" value="something">Car</option> <option id="two" value="anything">Plane</option> </select> Also, I ...

The implementation of jQuery within an included HTML file may not function properly

I'm looking to implement the use of includes for generic HTML elements in my project. However, I've run into an issue where including an HTML file causes jQuery to stop working. Here is a snippet of my code that illustrates the problem. Specifica ...

Move the footer to the bottom of the page

Is there a way to position my footer at the bottom of the screen and make it extend to the full width in a responsive manner? https://i.sstatic.net/19lSB.jpg Custom CSS for Footer: * { margin: 0; } html, body { height: 100%; } .page-wrap { min-heig ...

Can Chrome DevTools be used to manipulate the login process?

For a recent project, I utilized the login authentication logic from this GitHub repository as a reference: https://github.com/cornflourblue/angular-authentication-example In a situation where the backend was offline, I manually tweaked the frontend code ...

Issue with sender field in contact form and mailer function

Having issues with my contact form that has 3 fields and a textarea... I've used jQuery for validation and PHP to handle email sending. The contact form is functioning correctly, but the From field in the received emails is not displaying as expect ...

The jQuery.one() function fires off twice

Why am I experiencing a strange issue where the event bound with jQuery.one() gets triggered twice when I hit Enter in the input and then click outside the input within the same second? I thought that one() was meant to trigger at most once! $('.list& ...

Updating the value of a rails parameter with JavaScript

I am trying to enhance my search form by pre-populating the fields with the "last searched values" when a user submits a search. The search form and results are displayed on the same page, and if there is no previous search, the fields should show placehol ...

Tips for updating the content of a DIV element on a separate page

One question that often arises is how to change the content of a div element on a different page within a website. While it is common to use Ajax to dynamically update div content within the same page by fetching data from an external .html file, the proce ...

Unable to implement Bootstrap 5 Hamburger Animation - Looking for solutions

SOLVED I successfully resolved the issue :) The challenge was: $(function () { $('.navbar-toggler-button').on('click', function () { $('.animated-hamburger').toggleClass('open'); //Chrome expert mode ...

What is the best way to retrieve a response from a PHP file as an array through Ajax?

Seeking assistance in retrieving a complete address by entering the postal code in an HTML form textbox and clicking a button. The setup involves two files - one containing the ajax function and the other housing the PHP code. Uncertainty looms over whethe ...

Send both file and model data using AngularJS with FormData

When uploading an image using the file input type along with other user data, my model includes the User class: public class User { public int Id { get; set; } public string Name { get; set; } public int Rollno { get; set; } public byte[] ...

Showing a JavaScript variable on an HTML page

I have a challenge where I am attempting to showcase a variable using the code provided below: HTML: <div id="MyEdit">Money</div> JS: var price1 = 0; var current_value=document.getElementById("MyEdit").innerHTML; if (current_value == "msc" ...

Guide to verifying a field's value in a database

My web application requires three inputs from the user: UserName, FirstName, and Email. The goal is to verify if the entered username exists in the database and if so, validate the corresponding Firstname and email. If all three values are correct, a succ ...

sending a transformed javascript array from php to a javascript function

I'm looking for a way to pass a JavaScript array (that has been converted from PHP to JS) from PHP code to a JavaScript function. <?php // Convert PHP array to JavaScript array $php_array = array('he','hi','hello'); ...

What is the most effective method for disregarding undefined values?

Implementing a project using Vue.js and Vuex, I am retrieving data from an API that functions as a content-management system. The product class in the CMS possesses numerous properties that can be populated by the user, such as; { "title": &quo ...