How do I show a conditional image based on variable?
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
add a comment |
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
Try placing that particular script after thehtml
withresult
id. Let me know if that helps.
– AkshayM
Nov 12 '18 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 '18 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 '18 at 12:25
add a comment |
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
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
javascript html
asked Nov 12 '18 at 12:15
Ceesiebird
62111
62111
Try placing that particular script after thehtml
withresult
id. Let me know if that helps.
– AkshayM
Nov 12 '18 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 '18 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 '18 at 12:25
add a comment |
Try placing that particular script after thehtml
withresult
id. Let me know if that helps.
– AkshayM
Nov 12 '18 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 '18 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 '18 at 12:25
Try placing that particular script after the
html
with result
id. Let me know if that helps.– AkshayM
Nov 12 '18 at 12:18
Try placing that particular script after the
html
with result
id. Let me know if that helps.– AkshayM
Nov 12 '18 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 '18 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 '18 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 '18 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 '18 at 12:25
add a comment |
2 Answers
2
active
oldest
votes
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
Works like a dream! Thank you for your detailed response.
– Ceesiebird
Nov 12 '18 at 12:33
@Ceesiebird Glad this helped you.
– Jai
Nov 12 '18 at 12:35
add a comment |
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;
where are you getting result element? which is not there in your html.
– Sindhoor
Nov 12 '18 at 12:31
@Sindhoor it's ademo
, you can just ignoreresult
element, Ceesiebird reference this element in the Question.
– Ian Zhong
Nov 12 '18 at 12:38
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
Works like a dream! Thank you for your detailed response.
– Ceesiebird
Nov 12 '18 at 12:33
@Ceesiebird Glad this helped you.
– Jai
Nov 12 '18 at 12:35
add a comment |
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
Works like a dream! Thank you for your detailed response.
– Ceesiebird
Nov 12 '18 at 12:33
@Ceesiebird Glad this helped you.
– Jai
Nov 12 '18 at 12:35
add a comment |
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
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
answered Nov 12 '18 at 12:24
Jai
63.6k95479
63.6k95479
Works like a dream! Thank you for your detailed response.
– Ceesiebird
Nov 12 '18 at 12:33
@Ceesiebird Glad this helped you.
– Jai
Nov 12 '18 at 12:35
add a comment |
Works like a dream! Thank you for your detailed response.
– Ceesiebird
Nov 12 '18 at 12:33
@Ceesiebird Glad this helped you.
– Jai
Nov 12 '18 at 12:35
Works like a dream! Thank you for your detailed response.
– Ceesiebird
Nov 12 '18 at 12:33
Works like a dream! Thank you for your detailed response.
– Ceesiebird
Nov 12 '18 at 12:33
@Ceesiebird Glad this helped you.
– Jai
Nov 12 '18 at 12:35
@Ceesiebird Glad this helped you.
– Jai
Nov 12 '18 at 12:35
add a comment |
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;
where are you getting result element? which is not there in your html.
– Sindhoor
Nov 12 '18 at 12:31
@Sindhoor it's ademo
, you can just ignoreresult
element, Ceesiebird reference this element in the Question.
– Ian Zhong
Nov 12 '18 at 12:38
add a comment |
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;
where are you getting result element? which is not there in your html.
– Sindhoor
Nov 12 '18 at 12:31
@Sindhoor it's ademo
, you can just ignoreresult
element, Ceesiebird reference this element in the Question.
– Ian Zhong
Nov 12 '18 at 12:38
add a comment |
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;
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;
answered Nov 12 '18 at 12:27
Ian Zhong
544112
544112
where are you getting result element? which is not there in your html.
– Sindhoor
Nov 12 '18 at 12:31
@Sindhoor it's ademo
, you can just ignoreresult
element, Ceesiebird reference this element in the Question.
– Ian Zhong
Nov 12 '18 at 12:38
add a comment |
where are you getting result element? which is not there in your html.
– Sindhoor
Nov 12 '18 at 12:31
@Sindhoor it's ademo
, you can just ignoreresult
element, Ceesiebird reference this element in the Question.
– Ian Zhong
Nov 12 '18 at 12:38
where are you getting result element? which is not there in your html.
– Sindhoor
Nov 12 '18 at 12:31
where are you getting result element? which is not there in your html.
– Sindhoor
Nov 12 '18 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 '18 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 '18 at 12:38
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Try placing that particular script after the
html
withresult
id. Let me know if that helps.– AkshayM
Nov 12 '18 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 '18 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 '18 at 12:25