How do I show a conditional image based on variable?










1














I am trying to show a different image depending on the outcome of my JS app.



My HTML + JS looks as follows now.



 if (c == 2) 
document.getElementById('result').innerHTML = num + ' is a Prime number';
var imageshown = "OptimusPrime.gif"
else
document.getElementById('result').innerHTML = num + ' is NOT a Prime number';
var imageshown = "Megatron.gif"

}
</script>
<div class="container">
<form id="contact" method="post">

<h3>Prime-O-Tron</h3>
<h4>Please fill in a number in the field below and press "Calculate" to see if it is a prime number or not.</h4>

Please enter a number:
<input type="number" id="num" name="num" min="0" />
<input type="button" value="Find Prime Number" onclick="findPrime()" name="find" />
<div style="margin-top: 10px;" id="result"></div>
<img src= imageshown>


But it is not displaying any image, no matter if the outcome is from the if or the else.



How would I embed a different image based on the outcome of my JS ?










share|improve this question





















  • Try placing that particular script after the html with result id. Let me know if that helps.
    – AkshayM
    Nov 12 at 12:18










  • use show hide technique in js. you can easily track any html element and show or hide it. a little bit googling will help.
    – Tazbir Bhuiyan
    Nov 12 at 12:18










  • you can not use local variable like that. give id to img tag and set source in javascript. that works fine.
    – Sindhoor
    Nov 12 at 12:25















1














I am trying to show a different image depending on the outcome of my JS app.



My HTML + JS looks as follows now.



 if (c == 2) 
document.getElementById('result').innerHTML = num + ' is a Prime number';
var imageshown = "OptimusPrime.gif"
else
document.getElementById('result').innerHTML = num + ' is NOT a Prime number';
var imageshown = "Megatron.gif"

}
</script>
<div class="container">
<form id="contact" method="post">

<h3>Prime-O-Tron</h3>
<h4>Please fill in a number in the field below and press "Calculate" to see if it is a prime number or not.</h4>

Please enter a number:
<input type="number" id="num" name="num" min="0" />
<input type="button" value="Find Prime Number" onclick="findPrime()" name="find" />
<div style="margin-top: 10px;" id="result"></div>
<img src= imageshown>


But it is not displaying any image, no matter if the outcome is from the if or the else.



How would I embed a different image based on the outcome of my JS ?










share|improve this question





















  • Try placing that particular script after the html with result id. Let me know if that helps.
    – AkshayM
    Nov 12 at 12:18










  • use show hide technique in js. you can easily track any html element and show or hide it. a little bit googling will help.
    – Tazbir Bhuiyan
    Nov 12 at 12:18










  • you can not use local variable like that. give id to img tag and set source in javascript. that works fine.
    – Sindhoor
    Nov 12 at 12:25













1












1








1







I am trying to show a different image depending on the outcome of my JS app.



My HTML + JS looks as follows now.



 if (c == 2) 
document.getElementById('result').innerHTML = num + ' is a Prime number';
var imageshown = "OptimusPrime.gif"
else
document.getElementById('result').innerHTML = num + ' is NOT a Prime number';
var imageshown = "Megatron.gif"

}
</script>
<div class="container">
<form id="contact" method="post">

<h3>Prime-O-Tron</h3>
<h4>Please fill in a number in the field below and press "Calculate" to see if it is a prime number or not.</h4>

Please enter a number:
<input type="number" id="num" name="num" min="0" />
<input type="button" value="Find Prime Number" onclick="findPrime()" name="find" />
<div style="margin-top: 10px;" id="result"></div>
<img src= imageshown>


But it is not displaying any image, no matter if the outcome is from the if or the else.



How would I embed a different image based on the outcome of my JS ?










share|improve this question













I am trying to show a different image depending on the outcome of my JS app.



My HTML + JS looks as follows now.



 if (c == 2) 
document.getElementById('result').innerHTML = num + ' is a Prime number';
var imageshown = "OptimusPrime.gif"
else
document.getElementById('result').innerHTML = num + ' is NOT a Prime number';
var imageshown = "Megatron.gif"

}
</script>
<div class="container">
<form id="contact" method="post">

<h3>Prime-O-Tron</h3>
<h4>Please fill in a number in the field below and press "Calculate" to see if it is a prime number or not.</h4>

Please enter a number:
<input type="number" id="num" name="num" min="0" />
<input type="button" value="Find Prime Number" onclick="findPrime()" name="find" />
<div style="margin-top: 10px;" id="result"></div>
<img src= imageshown>


But it is not displaying any image, no matter if the outcome is from the if or the else.



How would I embed a different image based on the outcome of my JS ?







javascript html






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 12:15









Ceesiebird

62111




62111











  • Try placing that particular script after the html with result id. Let me know if that helps.
    – AkshayM
    Nov 12 at 12:18










  • use show hide technique in js. you can easily track any html element and show or hide it. a little bit googling will help.
    – Tazbir Bhuiyan
    Nov 12 at 12:18










  • you can not use local variable like that. give id to img tag and set source in javascript. that works fine.
    – Sindhoor
    Nov 12 at 12:25
















  • Try placing that particular script after the html with result id. Let me know if that helps.
    – AkshayM
    Nov 12 at 12:18










  • use show hide technique in js. you can easily track any html element and show or hide it. a little bit googling will help.
    – Tazbir Bhuiyan
    Nov 12 at 12:18










  • you can not use local variable like that. give id to img tag and set source in javascript. that works fine.
    – Sindhoor
    Nov 12 at 12:25















Try placing that particular script after the html with result id. Let me know if that helps.
– AkshayM
Nov 12 at 12:18




Try placing that particular script after the html with result id. Let me know if that helps.
– AkshayM
Nov 12 at 12:18












use show hide technique in js. you can easily track any html element and show or hide it. a little bit googling will help.
– Tazbir Bhuiyan
Nov 12 at 12:18




use show hide technique in js. you can easily track any html element and show or hide it. a little bit googling will help.
– Tazbir Bhuiyan
Nov 12 at 12:18












you can not use local variable like that. give id to img tag and set source in javascript. that works fine.
– Sindhoor
Nov 12 at 12:25




you can not use local variable like that. give id to img tag and set source in javascript. that works fine.
– Sindhoor
Nov 12 at 12:25












2 Answers
2






active

oldest

votes


















1














You can change the src instead of updating the variable's value. Also you might simplify like this:



 if (c == 2) 
msg = num + ' is a Prime number';
imageshown = "OptimusPrime.gif"
else
msg = num + ' is NOT a Prime number';
imageshown = "Megatron.gif"


var result = document.getElementById('result');
var img = result.nextElementSibling;

result.textContent = msg;
img.src = imageshown;
} // <--function closing





share|improve this answer




















  • Works like a dream! Thank you for your detailed response.
    – Ceesiebird
    Nov 12 at 12:33










  • @Ceesiebird Glad this helped you.
    – Jai
    Nov 12 at 12:35


















-1














try this,




html




 <img id='img' src= imageshown>



javascript




if (c == 2) 
document.getElementById('result').innerHTML = num + ' is a Prime number';
var imageshown = "OptimusPrime.gif"
else
document.getElementById('result').innerHTML = num + ' is NOT a Prime number';
var imageshown = "Megatron.gif"


document.getElementById('img').src = imageshown;





share|improve this answer




















  • where are you getting result element? which is not there in your html.
    – Sindhoor
    Nov 12 at 12:31










  • @Sindhoor it's a demo, you can just ignore result element, Ceesiebird reference this element in the Question.
    – Ian Zhong
    Nov 12 at 12:38










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262006%2fhow-do-i-show-a-conditional-image-based-on-variable%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can change the src instead of updating the variable's value. Also you might simplify like this:



 if (c == 2) 
msg = num + ' is a Prime number';
imageshown = "OptimusPrime.gif"
else
msg = num + ' is NOT a Prime number';
imageshown = "Megatron.gif"


var result = document.getElementById('result');
var img = result.nextElementSibling;

result.textContent = msg;
img.src = imageshown;
} // <--function closing





share|improve this answer




















  • Works like a dream! Thank you for your detailed response.
    – Ceesiebird
    Nov 12 at 12:33










  • @Ceesiebird Glad this helped you.
    – Jai
    Nov 12 at 12:35















1














You can change the src instead of updating the variable's value. Also you might simplify like this:



 if (c == 2) 
msg = num + ' is a Prime number';
imageshown = "OptimusPrime.gif"
else
msg = num + ' is NOT a Prime number';
imageshown = "Megatron.gif"


var result = document.getElementById('result');
var img = result.nextElementSibling;

result.textContent = msg;
img.src = imageshown;
} // <--function closing





share|improve this answer




















  • Works like a dream! Thank you for your detailed response.
    – Ceesiebird
    Nov 12 at 12:33










  • @Ceesiebird Glad this helped you.
    – Jai
    Nov 12 at 12:35













1












1








1






You can change the src instead of updating the variable's value. Also you might simplify like this:



 if (c == 2) 
msg = num + ' is a Prime number';
imageshown = "OptimusPrime.gif"
else
msg = num + ' is NOT a Prime number';
imageshown = "Megatron.gif"


var result = document.getElementById('result');
var img = result.nextElementSibling;

result.textContent = msg;
img.src = imageshown;
} // <--function closing





share|improve this answer












You can change the src instead of updating the variable's value. Also you might simplify like this:



 if (c == 2) 
msg = num + ' is a Prime number';
imageshown = "OptimusPrime.gif"
else
msg = num + ' is NOT a Prime number';
imageshown = "Megatron.gif"


var result = document.getElementById('result');
var img = result.nextElementSibling;

result.textContent = msg;
img.src = imageshown;
} // <--function closing






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 12:24









Jai

63.6k95479




63.6k95479











  • Works like a dream! Thank you for your detailed response.
    – Ceesiebird
    Nov 12 at 12:33










  • @Ceesiebird Glad this helped you.
    – Jai
    Nov 12 at 12:35
















  • Works like a dream! Thank you for your detailed response.
    – Ceesiebird
    Nov 12 at 12:33










  • @Ceesiebird Glad this helped you.
    – Jai
    Nov 12 at 12:35















Works like a dream! Thank you for your detailed response.
– Ceesiebird
Nov 12 at 12:33




Works like a dream! Thank you for your detailed response.
– Ceesiebird
Nov 12 at 12:33












@Ceesiebird Glad this helped you.
– Jai
Nov 12 at 12:35




@Ceesiebird Glad this helped you.
– Jai
Nov 12 at 12:35













-1














try this,




html




 <img id='img' src= imageshown>



javascript




if (c == 2) 
document.getElementById('result').innerHTML = num + ' is a Prime number';
var imageshown = "OptimusPrime.gif"
else
document.getElementById('result').innerHTML = num + ' is NOT a Prime number';
var imageshown = "Megatron.gif"


document.getElementById('img').src = imageshown;





share|improve this answer




















  • where are you getting result element? which is not there in your html.
    – Sindhoor
    Nov 12 at 12:31










  • @Sindhoor it's a demo, you can just ignore result element, Ceesiebird reference this element in the Question.
    – Ian Zhong
    Nov 12 at 12:38















-1














try this,




html




 <img id='img' src= imageshown>



javascript




if (c == 2) 
document.getElementById('result').innerHTML = num + ' is a Prime number';
var imageshown = "OptimusPrime.gif"
else
document.getElementById('result').innerHTML = num + ' is NOT a Prime number';
var imageshown = "Megatron.gif"


document.getElementById('img').src = imageshown;





share|improve this answer




















  • where are you getting result element? which is not there in your html.
    – Sindhoor
    Nov 12 at 12:31










  • @Sindhoor it's a demo, you can just ignore result element, Ceesiebird reference this element in the Question.
    – Ian Zhong
    Nov 12 at 12:38













-1












-1








-1






try this,




html




 <img id='img' src= imageshown>



javascript




if (c == 2) 
document.getElementById('result').innerHTML = num + ' is a Prime number';
var imageshown = "OptimusPrime.gif"
else
document.getElementById('result').innerHTML = num + ' is NOT a Prime number';
var imageshown = "Megatron.gif"


document.getElementById('img').src = imageshown;





share|improve this answer












try this,




html




 <img id='img' src= imageshown>



javascript




if (c == 2) 
document.getElementById('result').innerHTML = num + ' is a Prime number';
var imageshown = "OptimusPrime.gif"
else
document.getElementById('result').innerHTML = num + ' is NOT a Prime number';
var imageshown = "Megatron.gif"


document.getElementById('img').src = imageshown;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 12:27









Ian Zhong

544112




544112











  • where are you getting result element? which is not there in your html.
    – Sindhoor
    Nov 12 at 12:31










  • @Sindhoor it's a demo, you can just ignore result element, Ceesiebird reference this element in the Question.
    – Ian Zhong
    Nov 12 at 12:38
















  • where are you getting result element? which is not there in your html.
    – Sindhoor
    Nov 12 at 12:31










  • @Sindhoor it's a demo, you can just ignore result element, Ceesiebird reference this element in the Question.
    – Ian Zhong
    Nov 12 at 12:38















where are you getting result element? which is not there in your html.
– Sindhoor
Nov 12 at 12:31




where are you getting result element? which is not there in your html.
– Sindhoor
Nov 12 at 12:31












@Sindhoor it's a demo, you can just ignore result element, Ceesiebird reference this element in the Question.
– Ian Zhong
Nov 12 at 12:38




@Sindhoor it's a demo, you can just ignore result element, Ceesiebird reference this element in the Question.
– Ian Zhong
Nov 12 at 12:38

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262006%2fhow-do-i-show-a-conditional-image-based-on-variable%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto