I have a series of divs on my page and am having trouble selecting each div containing a specific string when the string is kept in an array. For example, if the array = ['foo', 'qux'], I would like to be able to select the div containing 'foo', and the div containing 'qux'.
<div class="word word1">foo</div>
<div class="word word2">bar</div>
<div class="word word3">baz</div>
<div class="word word4">qux</div>
The script
array = ['foo', 'qux'];
for (var i = 0; i < array.length; i++) {
array_text = array[i];
alert( $( "div:contains(array_text)" ).text() );
}
I have also used this script without success:
correct_matches = ['foo', 'qux'];
$.each(correct_matches, function(index, value){
alert( $( "div:contains(value)" ).text() );
}
);
I am using the alert only to ascertain whether or not the $( "div:contains(array_text)") selector is working properly. The selector is where I am running into problems: $( "div:contains('bar')" ) works as expected. Once I use the array_text variable, I get an alert with an empty string. Any suggestions? Thank you.