0

How can i write this Code in If Clause?

$('#message').removeClass().addClass((data.error === true) ? 'error' : 'success').text(data.msg).show(500);
royhowie
  • 11,075
  • 14
  • 50
  • 67
aldimeola1122
  • 806
  • 5
  • 13
  • 23
  • 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
  • 369,085
  • 72
  • 557
  • 602
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
  • 23,792
  • 9
  • 78
  • 104