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

209
Visualizações
Check if "data" attribute contains A PART of input value (jQuery)

With this way I only find the needed div, if the "data-content" contains the complete word.

var input = "TestText123";
var found = $("#list").find(".box[data-name='" + input + "']").addClass("found");

But I also want to find this div if my "input" just contains a correct part of it (min. 5 characters).

It's interesting, that this would work, if I don't use the "data" method (but I need it).

var found = $("#list").find(".box:contains('" + input + "')").addClass("found");

How to modify the "data" method in jQuery, like the way I need it?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Add an asterisk before the equal sign: [data-name*=...].

Side note: the argument of addClass should not have a dot, so just addClass("found"):

var input = "TestText123";
var found = $("#list").find(".box[data-name*='" + input + "']").addClass("found");
.found { background: yellow }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<ul id="list">
    <li class ="box" data-name="WhatTestText12345">should match</li>
</ul>

about 4 years ago · Santiago Trujillo 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