How to find a specific id within a list?

Asked

Viewed 129 times

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:

inserir a descrição da imagem aqui

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:

inserir a descrição da imagem aqui

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?

  • HTML is filled dynamically. There is only one <ul> in the HTML part. The dynamic value is actually an integer.

  • 1

    @Petherson take a look at my answer ;) it seems simple, but in case it doesn’t let me know

  • 1

    Notice this on the last line: $("#ul-paginacao li#" + parametro_id)... will fetch a read containing the id of the variable parametro_id. If id is on tag a inside the LI, the correct thing would be to have a space between li 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)

  • Man, it worked out here. Both SAM’s and Leonardo’s answers were useful to me. Now I don’t know which to accept.

1 answer

4


As a whole id is single that is, you can only have one id = "1" on your page, you could do so(much simpler):

function SetBackgroundRed(id){
   $("#"+id).css("background", "red");
}

pagina_atual = parseInt($.parseJSON(data).paginaatual);
$('#pagina-atual').val(pagina_atual);
SetBackgroundRed(pagina_atual) //Pelo que vimos pagina_atual = id

You select the object from that stretch $("#"+id), put in a function for you to reuse elsewhere.

If that’s not the problem please let me know ;)

  • It’s not working. These buttons aren’t dynamically created in php. I’ll update the post for you to see the code of how I create these buttons.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.