php - Javascript if and else statement not working
Get the solution ↓↓↓This page has HTML, PHP, Javascript, and jQuery. My goal is to cause the code to display the word Public if the value is 1 and display Private if the value is 0.
On the page I simply tested
<p class="title" style="text-align:left;"><strong>Privacy:</strong> {%=file.privacy||''%}</p>
This test displayed either a 1 or 0 depending on the database info so this told me the db query and needed information is available and accurate.
My next step I need to accomplish is to then make a javascript code that will compare if
{%=file.privacy||''%} == 1
and then display the word Public or if
{%=file.privacy||''%} == 0
then display the word Private.
Here is the code attempt which failed:
<script> if( file.privacy = 1 ){
document.write("<b>Public</b>");
}else{
document.write("<b>Private</b>");
}</script>
Thanks for the suggestions and your solution (adding another "=") fixed the javascript code above. I still have an issue here because I am nesting one script tag inside of another script tag. Time to research that for a solution...
Answer
Solution:
You're using assignment instead of comparison. Try this
<script> if( file.privacy == 1 ){
document.write("<b>Public</b>");
}else{
document.write("<b>Private</b>");
}
</script>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: installation failed, reverting ./composer.json and ./composer.lock to their original content
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.