Problem with '.data ()' and its value inside an if

2

I'm creating If 's so I do not lose the habit of using them, but when trying something a little different than what I usually do, an error occurs, if does not work. How can I fix it? I tried the following modes:

if($('.carousel-indicators li.active').data('slide-to').val() = 0){
    console.log('Teste');
};
ERRO : Uncaught ReferenceError: Invalid left-hand side in assignment

and

if($('.carousel-indicators li.active').data('slide-to') = 0){
    console.log('Teste');
};
ERRO : Uncaught TypeError: $(...).data(...).val is not a function
    
asked by anonymous 16.02.2017 / 12:11

2 answers

3

Two problems:

  • .data() already gives you a String , you do not need (nor should you) use .val()

  • Comparison should be done with == or === , use only = is an assignment.

Solution:

if($('.carousel-indicators li.active').data('slide-to') == 0){
    console.log('Teste');
};
    
16.02.2017 / 12:20
3

When you compare something you need to use == or === (strict comparison). The = is to assign value to any variable.

    
16.02.2017 / 12:16