Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

346
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda