Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

345
Visualizações
Select an element by particular pattern with jquery

I have a bunch of elements that contain data-testid attributes.

Think of them as something like this:

<div data-testid="my-page">
  <div data-testid="my-page-content"></div>
  <div data-testid="my-page-footer">
    <button data-testid="my-page-action-button">Do something</button>
    <button data-testid="my-page-cancel-button">Cancel</button>
  </div>
</div>

For one of my tests, I need to find the buttons through their data test id pattern.

As you can see, it is my-page-**-button.

I need to know how to adjust my selectors accordingly so that I grab the elements that begin with my-page and end with button.

So far, I've been doing:

$("[data-testid^='my-page-']")

Unfortunately, this will grab my-page-content (or my-page-footer). How can I grab all elements with data-testids like my-page-**-button?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

either include the button

button[data-testid^='my-page-']

or include the ends with

[data-testid^='my-page-'][data-testid$='button']

console.log($("button[data-testid^='my-page-']").length);

console.log($("[data-testid^='my-page-'][data-testid$='button']").length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-testid="my-page">
  <div data-testid="my-page-content"></div>
  <div data-testid="my-page-footer">
    <button data-testid="my-page-action-button">Do something</button>
    <button data-testid="my-page-cancel-button">Cancel</button>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Select with prefix AND Suffix .

$("[data-testid^='my-page-'][data-testid$='-button']")

RULE in general

  • Execulsive AND : do not keep space between selector(s)
  • Accumulative AND: put , between selector(s)

You need here the Execulsive AND

about 4 years ago · Juan Pablo Isaza Relatório

0

I think this does what you need:

$('[data-testid^=my-page-][data-testid $=button')

A working demo is below, let me know if you needed something else.


DEMO

// Select using start and end selectors
$('[data-testid^=my-page-][data-testid $=button').each( function() {
  console.log($(this).attr("data-testid"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-testid="my-page">
  <div data-testid="my-page-content"></div>
  <div data-testid="my-page-footer">
    <button data-testid="my-page-action-button">Do something</button>
    <button data-testid="my-page-cancel-button">Cancel</button>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda