0

The problem I'm having right now is that the content I want to append now is too to just creating it inside the main JavaScript file.

Is there a way that I can put this content inside some other HTML file and then call it with AJAX in the JavaScript file and append this content maybe in a loop to append it the number of times chosen from a select input tag?

Here is the select input code:

<label for="beneficiarios">Cantidad de beneficiarios</label>
<select name="beneficiarios" id="beneficiarios">
  <option value="0">0</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
</select>

Here is the content I want to append:

<div class="row beneficiario-info">
  <div class="col-md-6">
    <div class="form-group w-75">
      <label for="bene-nombreyapellido">Nombre y Apellido</label>
      <input type="text" name="bene-nombreyapellido" id="bene-nombreyapellido" class="form-control">
    </div>
    <!-- form group -->
    <div class="form-group w-75">
      <label for="bene-ci-numero">Cedula de identidad</label>
      <div class="input-group">
        <div class="input-group-prepend">
          <select name="bene-ci-tipo" id="bene-ci-tipo" class="custom-select">
            <option value="VE">V</option>
            <option value="EX">E</option>
          </select>
        </div>
        <!-- cedula tipo prepend -->
        <input type="text" name="bene-ci-numero" id="bene-ci-numero" class="form-control" maxlength="8">
      </div>
      <!-- input group -->
    </div>
    <!-- form group -->
  </div>
  <!-- col -->
  <div class="col-md-6">
    <div class="form-group w-75">
      <label for="parentezco">Parentezco</label>
      <select name="parentezco" id="parentezco" class="custom-select">
        <option value="">---------</option>
        <option value="hijo">Hijo</option>
        <option value="padre">Padre</option>
        <option value="hermano">Hermano o Hermana</option>
        <option value="conyugue">Conyugue</option>
      </select>
    </div>
    <div class="form-group">
      <label for="servicioadicional">Servicios Adicionales</label>
      <select name="servicios_beneficiario" class="custom-select" id="servicios_beneficiario" multiple="multiple">
        <option value="medico">Médico</option>
        <option value="odontologico">Odontológico</option>
        <option value="funerario">Funerario</option>
        <option value="vial">Víal</option>
      </select>
    </div>
  </div>
  <!-- col -->
</div>
<!-- row beneficiario -->

Here is the JavaScript code:

var number = 0;

$('#ctd-beneficiarios').on('change', function() {
  numero = $(this).val();
  beneficiarios_wrapper.html('');

  for (var i = 0; i < numero; i++) {

  }
}); 

beneficiarios_wrapper is the div where I want to append the content to.

Ivan
  • 11,733
  • 5
  • 35
  • 63
  • You can do it this way, https://stackoverflow.com/questions/4818854/javascript-import-html-is-it-possible but the extra round trip to the server to get more html, if used several times for different templates would be quite terrible. My suggestion is that inside of your html you create a "hidden" template and clone that, append it, and then show it. – Cody G May 17 '18 at 17:29

1 Answers1

1

Rather than storing it in a separate file and retrieving it via AJAX, use a hidden template like so:

$main = $('#main');
for(var i=0;i<5;i++){
  var $template = $('.template').clone();
  $template.removeClass('template');
  $template.find('.mainText').text("Template "+i);
  $main.append($template);
  $template.show();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
  <div class="template" style="display:none;">
    <span class ="mainText"></span>
  </div>
</div>

Or, as J. Titus has suggested:

$main = $('#main');
for(var i=0;i<5;i++){
  var template = document.getElementById('template').innerHTML;
  var $template = $(template)
  $template.find('.mainText').text('Template '+i)
  $main.append($template);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script id="template" type="text/template">
    <div>
      <div class="test">
      <span class="mainText">test</span>
      </div>
    </div>
</script>
<div id="main">
</div>
Cody G
  • 6,752
  • 2
  • 28
  • 40
  • Putting the template code inside `` will remove the requirement to use `display:none`. – J. Titus May 17 '18 at 17:40
  • Thanks man this helped me a lot.. but I'm having a problem with some of the content inside the template script.. I'm using a jQuery plugin to affect some of the content inside (using the multiselect plugin).. I'm now appending the content correctly but somehow the plugin is not affecting the content anymore .. my head is going to explode with this seriously – roger rengifo May 17 '18 at 18:40
  • I'm adding the script after I call the script of the plugin on my HTML and all my JS code is inside the $(document).ready function as well.. and I'm calling my multiselect function after the content is appended so... I don't know what;s the problem – roger rengifo May 17 '18 at 18:43
  • `.multiSelect('refresh')`? – Cody G May 17 '18 at 18:43
  • Oh I see. So if you manually put the elements in and then call `multiselect` it works,but if you append and then call it it doesn't? Your `multiselect` call is right after the append, right? not in a separate document.ready callback? – Cody G May 17 '18 at 18:45
  • Yes exactly.. I tried different things.. like getting the ID from the select input for the multiselect plugin after it was appended.. but I didn;t work.. so.. I think I'm gonna try the 'display:none" option you gave me – roger rengifo May 17 '18 at 18:58