-4

There are 2 images and a variable.

var score = '0';
<div class="parent">
  <id ="trophy" src="imageTrophy.jpg" style="display:none"/>
  <id ="progressing" src="imageprogress1.jpg"/>
</div>

How write a if statement shorthand to toggle between these 2 images depending on the score?

score == 10 ? $('.parent').find('img').toggle() : $('.parent').find('img').toggle();
Becky
  • 5,467
  • 9
  • 40
  • 73

2 Answers2

1

Try,

$('.parent').find('img').hide().eq(score == 10 ? 0 : 1).show();
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
1

If you are willing to change HTML, Use single img element and set its src on the basis of condition.

<div class="parent">
  <img src="" />
</div>

Script, It will set the 'src' property of img based on score variable value.

var score = '0';
$('.parent img').prop('src' , function(){
    return score == 10 ? 'imageTrophy.jpg' : 'imageprogress1.jpg';
});
Satpal
  • 132,252
  • 13
  • 159
  • 168