5
My page has a selectbox, with some segments, when a user selects a segment, through the event on change
jQuery, he takes the value of <option>
selected and inserts an Hidden input with the value of this option inside a div with append.
I need to make a loop for inside this particular div, and take the Hidden input element with a specific class, however, this input is inserted into the DOM after event with append.
if I loop for, and give a console.log it always returns 'Undefined'.
I need to take this input element in specific and see if the user entered the same 2x input with the same value to only then insert this input
I would like you to demonstrate to me a way to catch this element after the append.
here is an example of the code: http://jsfiddle.net/stzfd7t9/2/
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');
}
});
Why don’t you look up if that input already exists before to insert it?
– Sergio