0

I have a simple question. I want to make an if statement in this code (if(i=2){str += 'test';}):

    var data = response.DATA;
    var str = '<ul>';
    for (var I = 0; I < data.length; I++) {
        str += '<li><a href="#blog?blog_id=' +  data[I][0] + '"data-transition="slide">' +      data[I][1] + '</a></li>';
 if(I=2){str += 'test';}
        }
    str += '</ul>';
    $('#output').html(str);
}

When I do, it's placing the word 'test' not only at row 2, but on every row.

1
  • 1
    if(I==2) or if(I===2), I=2 is assigning 2 to I... Commented Jun 21, 2013 at 7:10

2 Answers 2

3

Use == or === to compare two values :

  if (I===2) {str += 'test';}

The result of I=2, which changes the value of I, is 2, which evaluates as true in an if.

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

1 Comment

better use this === for also beeing sure the type is correct ;)
1

You have a typo in your script:

if (I=2) ...

means that I is always 2 because you're assigning 2 to it.

Comments

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.