how do i convert array elements to lists in output using jquery?

This line

jQuery("p").text(arr);

effectively does this

jQuery("p").text(arr.toString());

And .toString() on an array is the same as arr.join(",") – if you don’t want a comma but something else, then be explicit:

jQuery("p").html(arr.join("<br/>"));

(using .html() as it now contains html)

Updated snippet:

$(document).ready(function() {
  var arr = [];

  $("#txtResult").on("click", function() {
    text = "";
    if (jQuery("#txtFirstNo").val().match(/^\d{5}$/, '')) {
      arr.push("one");
    }
    if (jQuery("#txtFirstNo").val().match(/^\d{5}$/, '')) {
      arr.push("two");
    }
    if (jQuery("#txtFirstNo").val().match(/^\d{7}$/, '')) {
      arr.push("three")
    }

    jQuery("p").html(arr.join("<br/>"));
    arr = [];

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="txtFirstNo" name="txtFirstNo" placeholder="Enter value" />
<br><br>
<input type="submit" id="txtResult" /><br>
<p id="txtPrint"></p>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top