0

Edit: It was not the same as the possible duplicate, as I already addressed the fact that the clone() method was not copying the element - as I suspected, it was going to be something really lame, the scope of the prependTo was too wide, and was copying the line into multiple spaces, resulting into the 'select' value being transferred to a wrong location in the web page.

I am pretty sure that the answer to my question will be really lame and simple, but I can't make this happen for the life of me.

Upon the completion of a form, I need to copy the entire line into a div for reviewing purposes before adding the info to my database.

So, upon completion, I make sure to copy the entire line, add a 'remove' button, and prependTo the reviewing div, however when I am able to copy everything else, the select statement does not copy with the selected value.

I've tried using plain clone(), and it wouldn't copy the value, and then tried saving the value on a variable, and then adding it to the cloned element (before prepending it) and it won't work either.

Here's the html code:

<div class="tab-pane fade p-3" id="trucks-pane" role="tabpanel" aria-labelledby="trucks-tab">
    <div class="container-fluid mt-2">
      <div class="border p-2 rounded">
        <h6 class="show-hidden-form" role="button">Add Truck(s)<span class="float-right"><i class="fas fa-chevron-down text-secondary"></i></span> </h6>
        <div class="new-elements-form" style="display: none">
          <small class="text-secondary"><i>Start typing the driver's info, rows will populate automatically</i></small>
          <div class="d-flex flex-wrap new-element custom-input-group">
            <div class="mr-1 mb-1">
              <input type="text" class="form-control custom-input-element" name="" value="" autocomplete="new-password" placeholder="First Name">
            </div>
            <div class="mr-1 mb-1">
              <input type="text" class="form-control custom-input-element" name="" value="" autocomplete="new-password" placeholder="Last Name">
            </div>
            <div class="mr-1 mb-1">
              <input type="text" class="form-control custom-input-element datepicker" name="" value="" autocomplete="new-password" placeholder="Date of Birth">
            </div>
            <div class="mr-1 mb-1">
              <input type="text" class="form-control custom-input-element" name="" value="" autocomplete="new-password" placeholder="License Number">
            </div>
            <div class="mr-1 mb-1">
              <input type="text" class="form-control custom-input-element datepicker" name="" value="" autocomplete="new-password" placeholder="Date License Issued">
            </div>
            <div class="mr-1 mb-1">
              <input type="text" class="form-control custom-input-element datepicker" name="" value="" autocomplete="new-password" placeholder="Date License Expires">
            </div>
            <div class="mr-1 mb-1">
              <select class="form-control custom-input-element" name="">
                <option value="">Select Type</option>
                <option value="B1">B1</option>
                <option value="CDL">CDL</option>
              </select>
            </div>
          </div>
          <hr>
          <div class="added-elements">

            <div class="d-none add-elements-button">
              <button type="button" class="btn btn-primary" name="button">Add Truck(s)</button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="container-fluid mt-2">
      <div class="border p-2 rounded">
        <h6>Registered Trucks</h6>
      </div>
    </div>
  </div>

And here's the Javascript code, that handles the cloning:

    if(all_filled){
      var delete_button = $("<div class='mr-1 mb-1 d-flex remove-element-line' role='button'><i class='far fa-times-circle text-danger align-self-center'></i></div>");
      var select_value = scope.find('.custom-input-group').find('select').val();
      var new_line = scope.find('.custom-input-group').clone();
      new_line.find('select').val(select_value);
      new_line.removeClass('custom-input-group').find('.custom-input-element').removeClass('is-valid').attr('disabled', true);
      new_line.prepend(delete_button);
      new_line.prependTo('.added-elements');
      scope.find('.custom-input-group').find('.custom-input-element').removeClass('is-valid is-invalid').val('').first().focus();
      scope.find('.add-elements-button').trigger('toggle-button');
    }

What am I doing wrong?

  • Have you used the debugger? At which line in the above code do results start to deviate from your expectations? – Lightness Races in Orbit Jan 13 '19 at 03:40
  • In the html, it doesn't populate the value on the select. I can't identify where it deviates. the 'select_value' stores the value correctly (I can print it to the console), and more so, if I do console.log(new_line.find('select').val()) the value is printed correctly, however, when it reaches html, it doesn't have any value selected. – Lalo Santos Jan 13 '19 at 03:51
  • Possible duplicate https://stackoverflow.com/q/742810/2150268 – Cue Jan 13 '19 at 04:33
  • Possible duplicate of [Clone isn't cloning select values](https://stackoverflow.com/questions/742810/clone-isnt-cloning-select-values) – Cue Jan 13 '19 at 04:34
  • Not a duplicate, I had already identified that the clone() method was not working, found the issue, was a noob oversight of not managing the scope of the prependTo() appropriately. – Lalo Santos Jan 13 '19 at 04:38
  • Can you create snippet to reproduce your issue? – Just code Jan 13 '19 at 04:39
  • Thank you @Justcode, I solved it already, there were multiple ‘.added-elements’, so it was pasting it in the last one. I fixed it by including it in the scope of the correct ‘.tab-pane’ – Lalo Santos Jan 13 '19 at 18:16

0 Answers0