0

I'm loading the select with the data from the database only when I click on the select like this:

var cars = 
    [ { colour: 'red'} 
    , { colour: 'white'} 
    , { colour: 'black'} 
    ] 

let x = 0;
const campos_max = 10000;
$('#add_field').click (function(e) {
  e.preventDefault(); 
  if (x < campos_max) {
    $('#listas').append(`<div class="teste"><div class="form-group col-md-2" style="width: 20.833333325%; flex: 0 0 20.833%;max-width: 20.833%;"><select class="form-control2 spoilart1 Reffee${x}" name="Ref[]"><option></option></select><span class="form-highlight"></span><span class="form-bar"></span><label class="label3" for="Ref"></label></div></div>`);
  }
});
$(document).on("click",".spoilart1",function() {
  var html = $(`.Reffee${x}`);    
        
  cars.forEach(element => {
    html.append(`<option value="`+element+`">`+element+`</option>`);
  });
  x++;
});
.form-control2 {
  width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="listas">
</div>

<button type="button" id="add_field" class="btn btn-warning caixa">
  <i class="fas fa-plus"></i>
</button>

The problem I have is that only when I click the second time does it show the data returned in the select.

The reason for this is because I set the size of the select in the css with this line width: 100% ;, but I wanted to keep the size defined in the select and show the data returned in the first click.

If is set max-width: 100%; already returns the data on the first click, but the select will adjust the size according to the returned data and I wanted the select to keep the size of 100% regardless of the data returned from the database.

Bruno
  • 801
  • 5
  • 11
  • Hello Pinto, could you explain why about why you want this to be done in 2 specific executions? As you could place you onclick function inside the create button function and it will work. – Jack Dane Jan 16 '21 at 01:06
  • @Jack You mean put onclick inside the select? – Bruno Jan 16 '21 at 01:21
  • yeah sorry it is kind of hard to explain as they are both click events. Look at this JS Fiddle and tell me what you think, there are a few things I think you should change just but it works for you. https://jsfiddle.net/Jack_Dane/up24svyo/ – Jack Dane Jan 16 '21 at 01:29
  • @Jack I understood your example, but I intend that whenever you click on select, make a new query to the database because you may have new data and if you press select when I click on the button to make the select appear, then I am left without that possibility. did I understand myself? – Bruno Jan 16 '21 at 01:50
  • that makes sense, I understand why you may want to do the two different requests now. – Jack Dane Jan 16 '21 at 01:51
  • @Jack if using onclick on select will solve my problem as you said initially? – Bruno Jan 16 '21 at 01:56

1 Answers1

0

Change from a click event to a focus event:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<style>
  .form-control2 {
    width: 100%;
  }
</style>

<div id="listas">
</div>

<button type="button" id="add_field" class="btn btn-warning caixa">
  <i class="fas fa-plus"></i>
</button>

<script>
  var cars =
      [ { colour: 'red'}
      , { colour: 'white'}
      , { colour: 'black'}
      ]

  let x = 0;
  const campos_max = 10000;
  $('#add_field').click (function(e) {
    e.preventDefault();
    if (x < campos_max) {
      $('#listas').append(`<div class="teste"><div class="form-group col-md-2" style="width: 20.833333325%; flex: 0 0 20.833%;max-width: 20.833%;"><select class="form-control2 spoilart1 Reffee${x}" name="Ref[]"><option></option></select><span class="form-highlight"></span><span class="form-bar"></span><label class="label3" for="Ref"></label></div></div>`);
    }
  });
  $(document).on("focus",".spoilart1",function() {
    var html = $(`.Reffee${x}`);

    cars.forEach(element => {
      html.append(`<option value="`+element+`">`+element+`</option>`);
    });
    x++;
  });
</script>

I have inspected the elements and they don't change until you click on the element.

Thanks,

Jack Dane
  • 402
  • 3
  • 12