Simple Javascript to change display content based on variable

hi,

I'm new to javascript but familiar with programming.

I want to change what's displayed on a simple webpage depending on a variable, what's a simple way to do this?

In VBA I could write, for example:

myPoints = 1

Select Case myPoints

    Case 0
        Debug.Print "You have no points."
    Case 1
        Debug.Print "You have just 1 point."
    Case Else
        Debug.Print "You have " & myPoints & " points."
End Select

Open in new window


Thanks
xeniumAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MishaProgrammerCommented:
try this:

<script type="text/javascript">
var myPoints = 2;
switch(myPoints) 
{
 case 0:
           alert("You have no points.");
           break;
case 1:
         alert("You have just 1 point.");
        break;
default:
        alert ("You have " + myPoints  + " points.");
}

    </script> 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
xeniumAuthor Commented:
Great thanks, that works fine.

I adapted it to write to the page:

<script type="text/javascript">
var myPoints = 2;

switch(myPoints) 
{
 case 0:
           myMessage = "You have no points.";
           break;
 case 1:
           myMessage = "You have just 1 point.";
           break;
 default:
           myMessage = "You have " + myPoints  + " points.";
}

document.write(myMessage);

</script> 

Open in new window



For my info, other ways to output: https://www.w3schools.com/js/js_output.asp
0
xeniumAuthor Commented:
For the record, example using document.getElementById(id)

<p id="myMessage"></p>


<script type="text/javascript">
var myPoints = 2;

switch(myPoints) 
{
 case 0:
           myMessage = "You have no points.";
           break;
 case 1:
           myMessage = "You have just 1 point.";
           break;
 default:
           myMessage = "You have " + myPoints  + " points.";
}

document.getElementById("myMessage").innerHTML = myMessage;

</script> 

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.