|
Mailing Lists
|
Home /
Groups /
Javascript
Please Help
Well,Kasey Clark 04/15/08 07:35 P Ok, so I am really bad at javascript, but I have to use it for a project for my computer class. This is my project: I am creating a personality quiz webpage. Each question has 4 different choices (each choice matches a different personality trait). I have done the HTML part and have all the text boxes where the user will put a number between 1 and 4 and the calculate button. Now I have to do the javascript part. I need javascript to add up the total of the 8 boxes(there are 8 questions) and have an alert pop up with their score and their personality description. So if their score is between 30 and 36 they have an outgoing personality, etc. Can anyone give me any advice on how to calculate the total score? Thanks! > Ok, so I am really bad at javascript, but I have to use it for a project for my computer class. This is my project: I am creating a personality quiz webpage. Each question has 4 different choices (each choice matches a different personality trait). I have done the HTML part and have all the text boxes where the user will put a number between 1 and 4 and the calculate button. Now I have to do the javascript part. I need javascript to add up the total of the 8 boxes(there are 8 questions) and have an alert pop up with their score and their personality description. So if their score is between 30 and 36 they have an outgoing personality, etc. Can anyone give me any advice on how to calculate the total score? Thanks! alert(document.getElementById('answer1').value + document.getElementById('answer2').value + document.getElementById('answer3').value + document.getElementById('answer4').value); this makes a couple of assumptions :) one assumption is that the form fields have an ID attribute each. if they don't, you can reference them via document.formname.fieldname.value the other assumption is that all of the values are actually numeric. there's a few ways to handle that... but let's see how far this gets you first. -- Evelyn the dog, having undergone further modification pondered the significance of short-person behaviour in pedal depressed, pan-chromatic resonance, and other highly ambient domains. "Arf," she said. Well, This is what I have so far (without the javascript): <html> <head><title>Kasey and Julia's Personality Quiz</title></head> <body BODY BGCOLOR="#FF00CC"> <center><h1><FONT FACE="kristen itc" SIZE="12" COLOR="#000000">Find Out Your Personality!</FONT> </h1> <h2><FONT FACE="kristen itc" COLOR="#000000">Choose the answer that best describes you!</FONT> </h2></center> <p1><FONT FACE="kristen itc"> 1.) If you had the weekend free would you do?<br /> 4=Do something spontanous with a few friends<br /> 3=Go camping with your other half<br /> 2=Movie night and pig out with best friend<br /> 1=Curled up on the couch with a good book/laptop<br /> <input type=text name="weekendBox" size=1><br /> <br /> 2.) If you had any amount of money to spend on a vacation where would you go?<br /> 4=Europe backpacking with a few close friends<br /> 3=Mountain biking in Canada with a pal<br /> 2=Shopping spree with your best friend in Tokyo<br /> 1=Escaping to the South Pacific<br /> <input type=text name="vacationBox" size=1><br /> <br /> 3.) On a weekly basis how many times do you hang out with friends?<br /> 4=5-7<br /> 3=3-4<br /> 2=2<br /> 1=1<br /> <input type=text name="friendBox" size=1><br /> <br /> 4.) What sport would you most likely engage yourself in?<br /> 4=Basketball/Football/Soccer<br /> 3=Baseball/Softball<br /> 2=Hiking/Tennis<br /> 1=Surfing/Swimming/Motorcross/Running<br /> <input type=text name="sportBox" size=1><br /> <br /> 5.) When you have a relationship what do you mostly do?<br /> 4=Hang out with groups of other friends<br /> 3=Go on double-dates<br /> 2=Hang out with partner only on weekends<br /> 1=Spend most of the time only together or on the phone<br /> <input type=text name="relationshipBox" size=1><br /> <br /> 6.) If you were stranded on an island who would you bring?<br /> 4-your other half and friends<br /> 3- A group of your closest friends<br /> 2-your other half<br /> 1-your pet<br /> <input type=text name="islandBox" size=1><br /> <br /> 7.) If you were asked to do a talent show what would you do?<br /> 4-An improvisation with some friends<br /> 3-An act from a scripted play with some friends<br /> 2-A karaoke duo<br /> 1-Read a poem aloud<br /> <input type=text name="talentBox" size=1><br /> <br /> 8.) When you go to school what do you look forward to doing?<br /> 4-Hanging out with friends between breaks<br /> 3-Being on the cell phone between breaks<br /> 2-Sitting next to a friend in class<br /> 1-having some quiet time in the library/lunch room<br /> <input type=text name="schoolBox" size=1><br /> <br /> <input type=button value="Calculate My Score!"</input> </font> </body> </html> What the plan is, is that if their score is 26-32 then they have a certain personality, if their score is 20-25 then it's another, if it is 14-19 it's another and if it is 8-13 then it's another. So how to I get an alert to pop up with their score and the personality description for that particular score? If it is too much to explain I totally understand, but I just thought I would see if I could get a little bit of help. Oh ya, since each of my text boxes has a different name is that what I would put in place of answer 1, 2, etc in your code below? > alert(document.getElementById('answer1').value + > document.getElementById('answer2').value + > document.getElementById('answer3').value + > document.getElementById('answer4').value); Thank you so much for your response I really appreciate it. I am so bad at this stuff and just can't seem to figure it out. ----- Excess quoted text cut - see Original Post for more ----- ----- Excess quoted text cut - see Original Post for more ----- your fields should be enclosed in <form></form> tags. let's say you have: <form name="foo"> <input type="text" name="bar" size="1" maxlength="1" /> </form> you'd reference that via: document.foo.bar.value if your elements have an ID attribute, you don't need to traverse the DOM, and you can reference them directly. e.g. <input type="text" name="bar" id="bar" size="1" maxlength="1" /> you'd reference that via: document.getElementById('bar') anyway... wrap your form in <form></form> tags, and use the notation above (document.formName.fieldName.value) to try and put something together. You can still use the alert() example i gave you. just use the formName.fieldName reference instead of the getElementById(). -- Evelyn the dog, having undergone further modification pondered the significance of short-person behaviour in pedal depressed, pan-chromatic resonance, and other highly ambient domains. "Arf," she said. Ok, so I enclosed each input tag with form tags and gave them each an ID name. So, I referenced them using this: document.getElementById('id name') and added them together (the original notation you gave me) and I got nothing. Is this notation suppose to be between the script tags? What do I put after the "onclick" part of the button? Because right now, nothing happens when I click the button. This is so confusing I don't know how anyone understands this haha. Plus, I have a really horrible teacher so the whole class feels the same way I do. Thanks for all your help! ----- Excess quoted text cut - see Original Post for more ----- > Ok, so I enclosed each input tag with form tags and gave them each an ID name. So, I referenced them using this: document.getElementById('id name') and added them together (the original notation you gave me) and I got nothing. Is this notation suppose to be between the script tags? What do I put after the "onclick" part of the button? Because right now, nothing happens when I click the button. This is so confusing I don't know how anyone understands this haha. Plus, I have a really horrible teacher so the whole class feels the same way I do. Thanks for all your help! show me what you have now. if it's live, send me a URL, otherwise paste the code at www.nomorepasting.com and let me know what the URL of the paste is. i'll take a look and give you whatever pointers i can, but i'm not just going to write it for you :) -- Evelyn the dog, having undergone further modification pondered the significance of short-person behaviour in pedal depressed, pan-chromatic resonance, and other highly ambient domains. "Arf," she said. I don't expect you to do it for me, I just wanted some tips. Here is the pasting link: http://www.nomorepasting.com/getpaste.php?pasteid=14618. And again, thank you for all your help, I really do appreciate it! ----- Excess quoted text cut - see Original Post for more ----- > I don't expect you to do it for me, I just wanted some tips. Here is the pasting link: http://www.nomorepasting.com/getpaste.php?pasteid=14618. And again, thank you for all your help, I really do appreciate it! no worries. just setting proper expectations :) couple of things: 1) the <form></form> tags should surround all the form fields. not a <form> tag for each field. you want <form> stuff stuff <input blah> stuff <input> <input> etc </form>. not *really* necessary since you're referencing the inputs via their IDs, but it's good... well, form to have the inputs surrounded by a single <form></form> block. 2) the reason nothing's happening when you click the button is because your onclick handler isn't calling anything. change this: <input type=button value="Calculate My Score!" onclick='()' to this: <input type="button" value="Calculate My Score" onclick="doCalculate();" /> 3) now that your button is calling a docCalculate() function, you need to actually have a doCalculate() function. so change your <script> accordingly: <script type="text/javascript"> function doCalculate() { alert(document.getElementById("weekendBox").value + document.getElementById("vacationBox" ).value + document.getElementById("friendBox").value + document.getElementById("sportBox").value) + document.getElementById("relationshipBox").value) + document.getElementById("islandBox").value) + document.getElementById("talenBox").value) + document.getElementById("schoolBox").value); } </script> that might work, might not. but it should do *something*. even if it generates an error, that's progress. Let me know what happens. -- Evelyn the dog, having undergone further modification pondered the significance of short-person behaviour in pedal depressed, pan-chromatic resonance, and other highly ambient domains. "Arf," she said.
|
May 19, 2013
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||