Change a class search in jQuery to a word search

Asked

Viewed 32 times

0

I’m sorry if I wasn’t clear on my question. I’m starting with JS and jQuery And my question is this: in the code below, is there any way that instead of searching for classes, searching for words?

when(function() {
    return jQuery != undefined && jQuery('.classe1, .classe2').length;
}) 
  • What exactly do you mean by searching for words? jQuery uses CSS selectors, so you can use any CSS selector rule to do your search. CSS selector reference: https://www.w3schools.com/cssref/css_selectors.asp

  • Man, I don’t know how to express myself very well in this situation, but I really need the word selection. In this case, if any, put words in place of '.Classe1'. classe2' I don’t really know if this is possible by simply typing the words didn’t work out, but I’m searching otherwise.

  • Try to describe better what you want to do, who knows the solution may be different from what you are trying.

  • I have an html that will look for these classes, and appear after them, but I wanted to do it by words I know my explanation is confusing, it was really personal

1 answer

0

Hello,

To search for string in attribute, you can use the Attribute = Value selector.

Documentation: https://www.w3schools.com/cssref/sel_attribute_value.asp

Example:

var texto1 = $('[class*="teste1"]').text();
var texto2 = $('[class*="teste5"]').text();
var texto3 = $('[class*="teste9"]').text();

$('#seletor-1').html(texto1);
$('#seletor-2').html(texto2);
$('#seletor-3').html(texto3);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="teste1 teste2 teste3">Lorem ipsum sit dolor ommet 1</p>
<p class="teste4 teste5 teste6">Lorem ipsum sit dolor ommet 2</p>
<p class="teste7 teste8 teste9">Lorem ipsum sit dolor ommet 3</p>

<hr>

<p id="seletor-1"></p>
<p id="seletor-2"></p>
<p id="seletor-3"></p>

In the case of this example, we used [class*="WORD"], where "class" is the attribute of the tag (could be any other attribute), "*=" is an operator indicating "contain" and "WORD" is the string to be searched for.

Browser other questions tagged

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