0

I am trying to match text to an array with a pause between each iteration. Is my if statement and array match incorrect?

var animals = ["turtle", "dolfin"];

setTimeout(function() {

  $("tr").each(function(){
    if($(this).$("td:eq(0)").text().inArray(animals)){
      $(this).$("button").hide();
    }
    else{
    }
  });

}, 5000);

<tr>
  <td>turtle></td>
  <td>lion</td>
  <td><button>correct</button></td>
</tr>

<tr>
  <td>cat></td>
  <td>dog</td>
  <td><button>correct</button></td>
</tr>
6
  • 1
    $(this).$("td:eq(0)") wut Commented Jan 31, 2018 at 23:32
  • I don't see anything in your question that would cause or attempt to cause a pause between iterations. Commented Jan 31, 2018 at 23:35
  • I wanted to only use the text in the first cell of each row. Commented Jan 31, 2018 at 23:40
  • @KevinB I'm with you on the $(this).$("td:eq(0)") in the if statement. But the setTimeout() will cause a 5 second delay between iterations, provided the syntax errors are fixed. Commented Jan 31, 2018 at 23:47
  • @user9263373 the setTimeout will cause a 5 second delay before any iterations occur, then they'll all happen at once. Commented Jan 31, 2018 at 23:47

2 Answers 2

3

There are some issues with your code:

Your tr is not within a table tag.

<tr>
^

You're using the jQuery symbol in wrong places

if($(this).$("td:eq(0)").text().inArray(animals)){
           ^

$(this).$("button").hide();
        ^

You need to compare the returned value from $.inArray() as follow:

$.inArray() > -1

You have an empty else

 else{
 }
 ^

Look this code snippet with those fixes:

var animals = ["turtle", "dolfin"];

setTimeout(function() {
  $("tr").each(function() {
    var text = $(this).children("td:eq(0)").text();
    if ($.inArray(text, animals) > -1) {
      $("#" + text).hide();
    }
  });
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>turtle</td>
    <td>lion</td>
    <td><button id='turtle'>correct</button></td>
  </tr>

  <tr>
    <td>cat</td>
    <td>dog</td>
    <td><button id='cat'>correct</button></td>
  </tr>
</table>

See? now the code is working well with your logic.

Sign up to request clarification or add additional context in comments.

3 Comments

excellent. However one additional change is needed. As @KevinB pointed out the setTimeout() needs to go inside the $("tr").each() loop to cause the delay between iterations.
@user9263373 Right, however like I said the code is working well with your logic, so, the OP should modify that :-)
Thank you for detailed explanation.
-2

Your if statement should be like this: if($(this).find("td:eq(0)").text().inArray(animals) > -1){

7 Comments

why should it be like that?
because of what you said
I just deleted what I said
@KevinB - where?
$(this).$ isn't a function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.