0

I have a script element with HTML content to be added dynamically to page I've tried adding it using jQuery with the following code:

let template = jQuery.validator.format($.trim($('#customTemplate').html()));
$($(template()).html()).appendTo('.custom-class');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js" integrity="sha512-37T7leoNS06R80c8Ulq7cdCDU5MNQBwlYoy1TX/WUsLFC2eYNqtKlV0QjH7r8JpG/S0GUMZwebnVFLPd6SU5yg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script type="text/html" id="customTemplate">
  <div class="col-sm-4">
    <div class="form-group">
      <label>Days: <span class="text-danger">*</span></label>
      <input type="date" name="Days" placeholder="Days..." class="form-control required data-required">
    </div>
  </div>
</script>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
InfZero
  • 2,944
  • 4
  • 24
  • 36

1 Answers1

-1
  • Why the trim?
  • Why not a template

This works

let template = $('#customTemplate')[0].content;
$('.custom-class').append(template)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>


<template id="customTemplate">
  <div class="col-sm-4">
    <div class="form-group">
      <label>Days: <span class="text-danger">*</span></label>
      <input type="date" name="Days" placeholder="Days..." class="form-control required data-required">
    </div>
  </div>
</template>

<div class="custom-class"></div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236