Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Array and IndexOf together?

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

like image 669
Sam Avatar asked Jan 23 '26 18:01

Sam


1 Answers

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!
}
like image 70
BenM Avatar answered Jan 25 '26 08:01

BenM