Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2013, 02:31 PM   PM User | #1
if_only
New to the CF scene

 
Join Date: Jan 2013
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
if_only is an unknown quantity at this point
multiple 'if' statements - please help a newbie!

Hey dudes!

Sorry to ask such a simple question but I have a time limit and need to know the answer quickly! My mate suggested a quick post on here!

I am working on a project for college - whilst i'm not studying Javascript I need to add some for functionality of my website. I know a bit to get by but seem to have got this wrong all day! I'll simplify it .....

I know that if I write .......

Code:
var userAnswer = prompt("Are you feeling OK today?");
if (userAnswer === "Yes")
{
	document.write("Great - glad to hear it");
}
else
{
	document.write("Oh dear! That is a shame!")
}

....... a prompt box appears asking "Are you feeling OK today?" and then if the user answers "Yes" .... one response is written and any other answer produces the second response.

What I need to know is - what if I want to write more than one response? For instance - what if I want there to be 4 different answers to the same question - with 4 different responses?

It makes sense to me to write ....

Code:
var userAnswer = prompt("Are you feeling OK today?");
if (userAnswer === "Yes")
{
	document.write("Great - glad to hear it");
}

if (userAnswer === "Not bad")
{
	document.write("Sorry to hear you are not feeling your best");
}

if (userAnswer === "so so")
{
	document.write("Great - glad to hear it");
}

else
{
	document.write("Oh dear! That is a shame!")
}
....... but this obviously does not work! I have hunted around but either am not searching for the right thing or am missing the point!

Can anyone please steer me in the right direction?

The project is obviously much harder than this but I need to get the basics down! Eventually each question will have a set of 4 radio buttons and then the answers to the questions delivered on a page ......

Ta in advance!
if_only is offline   Reply With Quote
Old 01-24-2013, 02:58 PM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,595
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
A prompt is always just a yes/no option. For multiple choice questions you need a form with radio buttons and validate the input (choice) upon submitting the form.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Users who have thanked VIPStephan for this post:
if_only (01-24-2013)
Old 01-24-2013, 03:39 PM   PM User | #3
mosquitobite
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 2
Thanked 1 Time in 1 Post
mosquitobite is an unknown quantity at this point
Seeing your codes and what you are trying to do can be also done with else if statements. Here is a sample from a site that I often go to http://www.w3schools.com/js/js_if_else.asp

I think another way you can do something similar is using switch statement, which can be little more elegant on making multiple if statement. Here is another link from the same site. http://www.w3schools.com/js/js_switch.asp

I newbie to javascript to, but there are a lot of useful site that explain the fundamental very well. Good luck with your javascript and keep at it .
mosquitobite is offline   Reply With Quote
Users who have thanked mosquitobite for this post:
if_only (01-25-2013)
Old 01-24-2013, 06:23 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
prompt provides three possible responses - to type in a string containing anything at all, to cancel and return nothing, or to turn off JavaScript completely and not return at all. In some browsers the last option is replaced by one that allows you to simply return nothing from this and all future prompts. It is an obsolete command with no real purpose in JavaScript for browsers more recent than Netscape 4.

document.write became obsolete when Netscape 4 died. All browsers since then have provided better ways to interact with the web page from JavaScript. The simplest one for beginners to use (even simpler than document.write) is innerHTML.

The w3schools JavaScript section is a history course demonstrating how JavaScript used to be written for use with Netscape browsers. The two guys who created the site have not updated it to cover the very different version of JavaScript used by modern browsers. If you are interested in how JavaScript used to be written then that site is a good resource but if you want to learn to actually write JavaScript then avoid it as much of what it teaches are things you need to stop using to get scripts to work properly in modern browsers.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 01-24-2013 at 06:26 PM..
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
if_only (01-25-2013)
Old 01-24-2013, 06:32 PM   PM User | #5
if_only
New to the CF scene

 
Join Date: Jan 2013
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
if_only is an unknown quantity at this point
Unhappy

Thank you for your reply mate. I totally understand what you mean .... doing it is another thing altogether. Very basically (I realise the code isn't all there - just trying to explain!) ... I want to write:

Q1
How you feeling today?

<input type="radio" name="feeling" value="not bad"
<input type="radio" name="feeling" value="so so"
<input type="radio" name="feeling" value="ok"
<input type="radio" name="feeling" value="great"

[hidden div appears over the original]

Q2
How you looking today?

<input type="radio" name="looking" value="ugly"
<input type="radio" name="looking" value="fair"
<input type="radio" name="looking" value="quite good"
<input type="radio" name="looking" value="stunning!"

[hidden div appears over the last]

There will be 3 questions and then finally the final hidden div will deliver the results to a form which can be emailed to me.

I've found a few solutions and have been trying to develop some code that a kind coder gave me but no luck!

To make things worse - I actually need it to eventually be cleverer than this ...... I don't actually want to know what they have selected. Each selection will be given a code and when the 3 selections are put together I need it to recommend a specific answer. In this case Feeling "so so" and Looking "Ugly" may suggest something like "Go for a Spa day!" ........ Each combination will be unique.

Is there anyway you (or anyone) can start me off?! I'm going crazy!
if_only is offline   Reply With Quote
Old 01-24-2013, 07:13 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
My only sensible suggestion is ... learn Javascript. Presumably you would not attempt to service or repair a machine or appliance without any experience or understanding of how it worked. You would think much of a law student who attempted to carry out a surgical operation.

You will need to use CSS to position your divs.

"... a form which can be emailed to me."
What would be the point of that when your questions are so trivial (pointless even)? In any case you will need server side scripting to send the contents of a web form by email.

Modern browsers no longer accept mailto: as a form action - they simply open the email program (if any) and ignore the form. And many users will (sensibly) not wish to reveal their email address.

document.write() has been obsolete since Netscape 3 passed away 10+ years ago. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

prompt() is also obsolete. And what if your user enters "yes" instead of "Yes"? Radio buttons are better.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 01-24-2013 at 07:19 PM..
Philip M is offline   Reply With Quote
Old 01-24-2013, 09:22 PM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by if_only View Post
Q1
How you feeling today?

<input type="radio" name="feeling" value="not bad"
<input type="radio" name="feeling" value="so so"
<input type="radio" name="feeling" value="ok"
<input type="radio" name="feeling" value="great"

[hidden div appears over the original]

Q2
How you looking today?

<input type="radio" name="looking" value="ugly"
<input type="radio" name="looking" value="fair"
<input type="radio" name="looking" value="quite good"
<input type="radio" name="looking" value="stunning!"

[hidden div appears over the last]

There will be 3 questions and then finally the final hidden div will deliver the results to a form which can be emailed to me.
At least with that you have a starting point for your code. With what you had in your original post there wasn't anything that was relevant to what you are trying to do.

If you place all those radio button groups into a form that has a form2mail script attached to the action attribute then at lest you can get their selections mailed to where ever you want. Once you get that much working then you can look at what else you need to add to get it doing the rest of what you want.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:42 PM.


Advertisement
Log in to turn off these ads.