My page has a selectbox, with some segments, when a user selects a segment, through the on change
event of jQuery, it takes the value of the selected <option>
and inserts an input hidden with the value of this option within a div with append.
I need to do a for loop within that particular div, and get the input element hidden with a specific class, however, this input is inserted into the DOM after event with append.
If I make a for loop, and I give a console.log it always returns 'undefined'.
I need to get this input element in specific and see if the user entered the same input 2x with the same value to only then insert this input
I would like you to show me a way to get this element after append.
Here is an example of the code: link
HTML:
<label for="segmento">selecione um segmento</label><br>
<select name="" id="segmento">
<option value="industria">industria</option>
<option value="tecnologia">tecnologia</option>
<option value="telecomunicações">telecomunicações</option>
</select>
<div id="result" class="filtro">
</div>
JAVASCRIPT:
/**
* limite de filtros
* padrão: 0;
*/
var filterLimit = 0;
/**
* container das etiquetas e input
*/
var filterContainer = $('#result');
var inputList = $('#result input[class="segment"]');
// quando uma opção for selecionada, adiciona a etiqueta
$('#segmento').on('change', function() {
filterLimit++;
var segValue = $('#segmento option:selected').text();
if( filterLimit < 4 ) {
// insere loop for aqui
for( var i=0; i<= inputList.length; i++ ) {
console.log(inputList)
}
// insere o elemento no DOM
filterContainer.append('<input type="hidden" name="segment[]" value="'+ segValue +'" class="segment">');
// insere a etiqueta
filterContainer.append('<div class="etiqueta">'+ segValue +' <a href="#" id="remove">x</a></div>');
} else {
alert('você só pode selecionar 3 filtros para segmento, remove um ou mais filtros e tente novamente');
}
});