5
I’ll try to summarize.
I have a list in which I have elements with various ids.
I would like to select a specific element within that list. Only that the ID value that will be searched for will be filled dynamically.
How do I select this element ?
Follow an example code:
I’m tempted to select that way:
pagina_atual = parseInt($.parseJSON(data).paginaatual);
$('#pagina-atual').val(pagina_atual);
$("#ul-paginacao li a:contains('" + pagina_atual + "')").css("background", "red");
But this is happening:
You are selecting all elements that have id that contain '1'. (Can be 1,144, 145, 194, etc ...)
BUTTON CREATION CODE
<?php
if($i == $paginaAtual || $i == ($paginaAtual-1) || $i == ($paginaAtual+1)){
$botoes .= "<li class='page-item'>";
$botoes .= "<a class='btn btn-sm btn-primary botao-de-filtro-numerico' href='' id=b".$i.">".$i."</a>";
$botoes .= "</li>";
}
//PARTE QUE envio o JSON do PHP para o retorno do AJAX
$retorno = array();
while($linha = mysqli_fetch_object($operacao_consulta))
{
$retorno[] = $linha;
}
$objeto = array(
'botoes' => $botoes,
'dados' => $retorno,
'paginaatual' => $pg_atual,
'quantidadebotoes' => $totalDePaginas,
'totalderegistros' => $totalDeRegistros
);
echo json_encode($objeto);
?>
NOTE: This $i variable comes from a php for
JAVASCRIPT CODE
//Trás o retorno do banco com todos os botões (<li><a></a></li>) que serão mostrados na paginação
paginacao += $.parseJSON(data).botoes;
//paginacao += "<li class='page-item'><a class='btn btn-sm bg-light text-primary border-primary' href=''>Próximo</a></li>";
$('#registros-atividades').html(atividades);
//Insere os botões na UL da paginação
$('#ul-paginacao').html(paginacao);
//Coloca no input hidden a página que está sendo mostrada na paginação
pagina_atual = parseInt($.parseJSON(data).paginaatual);
$('#pagina-atual').val(pagina_atual);
//Conta o número de botões que foi retornado no banco de dados.
contador_de_botoes = parseInt($.parseJSON(data).quantidadebotoes);
//Coloca esse valor no input hidden
$('#quantos-botoes-tem-na-pagina').val(contador_de_botoes);
//Total de registros retornados
total_registros = parseInt($.parseJSON(data).totalderegistros);
$('#total-de-registros').html(total_registros);
//$("#ul-paginacao li a:contains('" + pagina_atual + "')").css("background","Indigo");
parametro_id = pagina_atual.toString();
$("#ul-paginacao li#" + parametro_id).css("background", "red");
Could you help me ?
Put the HTML code you currently have. Also, the dynamic value would be for example an integer, as
1
?– Luiz Felipe
HTML is filled dynamically. There is only one <ul> in the HTML part. The dynamic value is actually an integer.
– Gato de Schrödinger
@Petherson take a look at my answer ;) it seems simple, but in case it doesn’t let me know
– Leonardo Bonetti
Notice this on the last line:
$("#ul-paginacao li#" + parametro_id)
... will fetch a read containing the id of the variableparametro_id
. If id is on taga
inside the LI, the correct thing would be to have a space betweenli
and#
, thus:$("#ul-paginacao li #" + parametro_id)
... but that doesn’t make sense since an id should be unique, it could do so:$("#" + parametro_id)
– Sam
Man, it worked out here. Both SAM’s and Leonardo’s answers were useful to me. Now I don’t know which to accept.
– Gato de Schrödinger