-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,217
  • 9
  • 33
  • 68

2 Answers2

1

Try,

$('.parent').find('img').hide().eq(score == 10 ? 0 : 1).show();
Rajaprabhu Aravindasamy
  • 64,912
  • 15
  • 96
  • 124
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
  • 129,808
  • 12
  • 152
  • 166