0

How can i write this Code in If Clause?

$('#message').removeClass().addClass((data.error === true) ? 'error' : 'success').text(data.msg).show(500);
royhowie
  • 10,605
  • 14
  • 45
  • 66
aldimeola1122
  • 786
  • 4
  • 11
  • 22
  • possible duplicate of [What does this symbol mean in JavaScript?](http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript) – j08691 Apr 17 '15 at 00:00

2 Answers2

2
var message = $('#message').removeClass();
if (data.error === true)
{
    message.addClass('error');
}
else
{
    message.addClass('success');
}
message.text(data.msg).show(500);

You could also put all those calls in the if-cases, but then you’d have to repeat the code all the time, so I split it up and used a local variable.

poke
  • 307,619
  • 61
  • 472
  • 533
1

This is the conditional operator in javascript.

(data.error === true) ? 'error' : 'success'

means if the first part is true data.error === true then you return 'error' elso you return 'success'

You can find more info here

koopajah
  • 19,437
  • 8
  • 62
  • 93