0
I have this script in html
and jquery
searching in the database what the user wrote in the input
<div class="form-group col-md-8" id="dynamicDiv1" >
<label for="categoria">Categoria</label>
<input type="search" name="categoria[]" autocomplete="off" class="form-control" id="busca" placeholder="Buscar por categoria">
<div class="col-md-12 rm_padding">
<ul class="list-group">
</ul>
</div>
</div>
and shows what comes from the database in this section
<div class="col-md-12 rm_padding">
<ul class="list-group">
</ul>
</div>
//Busca categoria dinamicamente
$(function () {
$('#busca').keyup(function () {
var pesquisa = $(this).val();
$.post('pesquisa.php', {categoria: pesquisa}, function (r) {
$('.list-group').html(r);
});
});
$('.list-group').delegate('li', 'click', function () {
var texto = $(this).text();
$('#busca').val(texto);
$('.list-group').html('');
});
$('body').click(function (event) {
if (event.target.id !== 'busca') {
$('.list-group').html('');
}
});
});
My problem is the input in which the user type it is dynamic or a field always appears as required but it can add 3 more fields
script to add fields
var contador = 0;
var limite = 3;
$(function () {
var scntDiv = $('#dynamicDiv1');
$(document).on('click', '#addInput1', function () {
if (contador < limite) {
$('<div id="dinamic">' +
'<input type="search" id="busca" required="" name="categoria[]" class="form-control" placeholder="Adicionar Categoria" /> ' +
'<div class="col-md-12 rm_padding"><ul class="list-group"></ul></div>'+
'<br><a class="btn btn-danger btn-sm left" href="javascript:void(0)" id="remInput">' +
'<span class="glyphicon glyphicon-minus " aria-hidden="true"></span> ' +
'Remover Categoria' +
'</a>' +
'</div>').appendTo(scntDiv);
contador++; // incremento do contador
}
return false;
});
$(document).on('click', '#remInput', function () {
$('#dinamic').remove();
if (contador > 0)
contador--; // remover do contador tb
return false;
});
});
how do I make for dynamic research to also work in those fields that are created dynamically.
But in case the return coming from the bank would be applied to all fields?
– fabricio
As long as they’re tag kids
<body>
(what they will be) will apply to all inputs (you can replace the 'input' there with 'form-control' or even '#search', but the idea of having several elements with the same ID is pretty bad)– Thiago Santos
Ai that ta the problem I need that each dynamic field can make its search in the bank
– fabricio
What’s the problem? Dynamic search will work on dynamically created items.
– Thiago Santos
my problem is that dynamic search does not work in the dynamic fields
– fabricio
@Did you understand my answer? Change
$('#busca').keyup(function () {
for$('body').delegate('#busca', 'keyup', function(){
that your search will work in the dynamic fields– Thiago Santos
Sorry had not understood. Now it worked however when I click on the result it does not fill in the input
– fabricio
It is because you have many elements called #search, you would have to do so, add this code:
var pesquisa = $(this).val(); 
var buscaAtual = $(this);
and replaces here$('#busca').val(texto);
forbuscaAtual.val(texto);
– Thiago Santos
ReferenceError: buscaAtual is not defined[Learn More]
, This is me returning that error on the console. sorry so many questions is that I’m getting bored eating to learn js– fabricio
I have made Global your search variable and you can try, any doubt we will continue this discussion in chat.
– Thiago Santos