I'm really close to having this working. I need to see if part of the string of text inside div containers of a certain class contain one of the items inside an array.
Here is what I tried in order to try to use indexof
to try to search the string instead of inArray
(this code doesn't work):
var arr = ['item1', 'item2', 'item3'];
$('.mydiv').each(function() {
if(arr.indexOf(($(this).text())!==-1 ) {
// do something like functions, increments, and other things
}
};
Here is an example html. In this example, the expected result is that three of these divs should result in it being found in the array. You can see item1
for example in the second div, but for this div the query code in // do something
should be run. (Note that there are some other characters in the string like colons and slashes.)
<div class="mydiv">3.c:e/e.x 1w</div>
<div class="mydiv">0/item1:e</div>
<div class="mydiv">c3/i:e.ra7 item3</div>
<div class="mydiv">9.m:c/e1x</div>
<div class="mydiv">item2:2/w</div>
I tried the inArray
code but I discovered that it only worked on an EXACT match.
For example, the below code does not work because in the example html below, only the first div which is an exact match works, but not the second div. The required result is that both the divs in the example below containing item1
should both work, but of course the third div which doesn't contain anything in the array should not work as items in the array are not found in the text.
$('.mydiv').each(function() {
if($.inArray($(this).text(),arr)!==-1 ) {
// do something
}
};
<div class="mydiv">item1</div>
<div class="mydiv">miw0/item1w:e/doi</div>
<div class="mydiv">something else</div>
How can I get this to work so that if any of the array items are found in any part of the text inside the div, then the code will work and it will // do something
?
Please use the lease code and also please do not use a for
loop because I need to use functions inside the code.