I am trying to create an array of strings and use indexOf function to check if the text exists on <td> or not, if it does I want it to execute else do nothing?
for .e.g One exists on <td> so indexOf recognizes that executes
The problem is this code is not working at the moment it was working when I was not using an array.
UPDATES- Thanks that indexOf works fine but the way I have used an array on var str doesn't seem to work.
HTML
<tbody>
<td><a href='#'>One</a></td>
<td>No.</td>
</tbody>
Jquery
var array = ['One', 'Two', 'Three', 'Four'];
var str = $('td:contains(" + array + ")').text();
if(array.indexOf(str) > -1){
console.log('Array works');
}
Any help will be much appreciated
You're using it the wrong way around. In this case, you want the indexOf() prototype of Array, not string, so your logic should be:
if( array.indexOf(str) > -1 )
{
// Do stuff!
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With