JavaScript - Need Help Summing If Statements
The issue I am having is with my sumAll function:
Code: <script type="text/javascript"> /* <![CDATA[ */ function addNumbers() { a = parseInt(document.getElementById('txtFirst').value); b = parseInt(document.getElementById('txtSecond').value); c = parseInt(document.getElementById('txtThird').value); d = parseInt(document.getElementById('txtFourth').value); ansE = document.getElementById("Sum"); ansE.value = '$' + (a + b + c + d); x = (a + b + c + d); if ( x >= 100000) { document.getElementById('Bonus').value = '$' + 1200; } else if ( x > 50000 && x<100000) { document.getElementById('Bonus').value = '$' + 800; } else { document.getElementById('Bonus').value = '$' + 0; } if (document.getElementById('Blue').checked == true && document.getElementById('Gala').checked == true) { document.getElementById('Committee').value = '$' + 800; } else if (document.getElementById('Blue').checked == true && document.getElementById('Gala').checked == false) { document.getElementById('Committee').value = '$' + 350; } else if (document.getElementById('Blue').checked == false && document.getElementById('Gala').checked == true) { document.getElementById('Committee').value = '$' + 450; } else { document.getElementById('Committee').value = '$' + 0; } } function sumAll() { y = parseInt(document.getElementById('Bonus').value); z = parseInt(document.getElementById('Committee').value); total = document.getElementById("Total"); total.value = (y + z); } function funcCalc() { addNumbers(); sumAll(); } /* ]]> */ </script> I'm really new to this and I'm trying to figure out how to sum the two values from if statements. ParseInt is returning NaN. Before that, it was adding my results together as strings. Is there an easy way to add these together without creating a very complicated if statement to total these up? Thanks! Similar Tutorialsi have a form with several headings, then for each heading i have a list of items with a select box filled with numbers. For each heading a maximum of items is allowed, so the user should change the values in the dropdowns and not be allowed to continue until they are less than or equal to their maximum limit. To complicate this, there will be more than one headings on the page, but each one with a different name/id. Eg: Heading 1 Title 1 DROPDOWN1 Title 2 DROPDOWN2 Title 3 DROPDOWN3 So here the max is 10, so the values cannot exceed 10 Heading 2 Title 1a DROPDOWN1 Title 2a DROPDOWN2 Title 3a DROPDOWN3 Title 4a DROPDOWN4 So here the max is 10, so the values cannot exceed 14 thanks I have trouble with summing variable zbroj, it is summed like it is string, and i dont have any idea why. Help please! Code: function zbrojibode() { var i=0; var zbroj=new Number(0); while((document.getElementById('bodtxt5'+i).value)){ if((document.getElementById('bodtxt5'+i).value!="") || (document.getElementById('bodtxt5'+i).value!="NaN")) zbroj+=parseFloat(document.getElementById('bodtxt5'+i).value).toFixed(2); i++; } document.getElementById('ukupno').value=zbroj; } I'm new to js and have been going crazy trying to get this to work. I need the script to pull the values in the class selected, then spit out the sum of them. It can't be that hard! This is what I have and it just won't run. I'm working in a intra environment, so I'm not too worried about cross-browser compatibility. Any help would be appreciated. Thank you. <html> <head> <script> function findTheOddOnes() { var theOddOnes = document.getElementsByClassName("odd"); for(var i=0; i<theOddOnes.length; i++){ sum += theOddOnes[i].textContent; alert(sum); } } </script> </head> <body> <h1>getElementsByClassName Test</h1> <p class="odd">1000</p> <p>This is an even para.</p> <p class="odd">1000</p> <p>This one is not odd.</p> <form> <input type="button" value="Find the odd ones..." onclick="findTheOddOnes()"> </form> </body> </html> Below is the script and form fields I am working with. What I want to do is sum the two textbox fields and have the result show in the total textbox. The code works fine and the total textbox is updated with the value of form1.basic. The problem occurs when I add the "+ parseInt(document.form2.supporter.value)" code in the script section. What am I doing wrong? Thank you for your time and help. Code: <script type="text/javascript"><!-- function updatesum() { document.frmtotal.total.value = parseInt(document.form1.basic.value) + parseInt(document.form2.supporter.value)} //--></script> <tr> <td width="111"><form id="form1" name="form1" method="post" action=""> <label for="basic">$</label> <input name="basic" type="text" id="basic" size="7" maxlength="7" onchange="updatesum(this.form)" /> </form></td> </tr> <tr> <td><form id="form2" name="supporter" method="post" action=""> <label for="supporter">$</label> <input name="supporter" type="text" id="supporter" size="7" maxlength="7" onchange="updatesum(this.form)" /> </form> Hello, I'm working on a project that involves extracting values from a series of select boxes and then working out the result. The select boxes contain the following: Code: <select name="select_Col1_Row1"> <option value="blue">Blue</option> <option value="green">Green</option> <option value="red>Red</option> </select> <select name="select_Col1_Row2"> <option value="blue" selected="selected">Blue</option> <option value="green">Green</option> <option value="red>Red</option> </select> <input type="text" name="RedSum"> <input type="text" name="BlueSum"> <input type="text" name="GreenSum"> </select> As you can see by my code, I'm early days at this and guessing the steps on the way. I tried using getElementsByTagName on the select and then on the option but had no luck with that. The code below does a wonderful job of returning blue for both select statements... Code: <script type="text/javascript"> function getElements() { var table = document.getElementById("x"); var x = table.getElementsByTagName("option"); for (var i = 0; i < x.length; i++) { var status = x[i].getAttribute("selected"); alert(status); if (status == "selected") { var statushcap = x[i].getAttribute("id"); var statusvalue =x[i].getAttribute("value"); if (statusvalue == "blue") { alert(statusvalue); } } } alert(x.length); } </script> If someone could suggest some way to map the select tag and then extract its option value to being the user selected value, I'd most appreciate it. I can probably do the rest of it. Can you extract additional information from the id of the option that is selected as I'd like to sum additional information from the user's choice. Best, John Im trying to figure out why this code won't work. Unsuccessfully. Sorry, didn't know how to use the "CODE" tag. <html> <head> <script type:"text/javascript"> var BGColor = prompt("Would you like the background of the page to be red, green, or blue?","") </script> </head> <body> <script type:"text/javascript"> if (BGColor == "red") { document.write('<body bgcolor= "red">The body of this page is RED. Press F5 to restart!') } else if (BGCOlor == "green") { document.write('<body bgcolor= "green">The body of this page is GREEN. Press F5 to restart!') } else if (BGColor == "blue") { document.write('<body bgcolor= "blue">The body of this page is BLUE. Press F5 to restart!') } </script> </body> </html> Code: <html> <script> // Author: L Freeman // Date: 11/10/10 // Purpose: Calculate student discount/vat reductions var price = 0; var vat = 17.5; var discount = 10; var reduction = 0; var deduction = 0; var reductionVAT = 0; var priceDiscount = 0; var priceNoVATDiscount = 0; // Calculate new price price = prompt("Enter price:"); // Calculate reduction reduction = (price/100) * discount; priceDiscount = (reductionVAT-reduction); priceNoVATDiscount = (price - reduction); // Calculate vat deduction deduction = (price/100) * vat; reductionVAT = (price - deduction); // Is VAT and/or a discount required? var answer = confirm ("VAT required?") if (answer) { alert ("Price = " + price + ", VAT = " + deduction + ", New Price is: " + reductionVAT) var answer = confirm ("Discount Required?") if (answer) { alert ("Price = " + reductionVAT + ", Discount = " + reduction + "New price is: " + priceDiscount) else alert ("No Discount Required. Price is: " + reductionVAT) } else alert ("No VAT required.") var answer = confirm ("Discount Required?") { if (answer) alert ("Price = " + price + ", Discount = " + reduction + "New price is: " + priceNoVATDiscount) else alert ("No Discount Required. Price is: " + price) } } </script> </html> I am having trouble getting this to work properly. What I am trying to do is come up with a simple vat deduction + student discount calculator. I cannot seem to get it working. Here is the code, in full: <htmll"> <head><title></title> <script type="text/javascript"> <!-- HIDE FROM INCOMPATIBLE BROWSERS function checkGrade(grade, value) { switch (grade) { case "A": if(grade == '90') window.alert("Your grade is excellent."); break; case "B": window.alert("Your grade is good."); break; case "C": window.alert("Your grade is fair."); break; case "D": window.alert("You are barely passing."); break; case "F": window.alert("You failed."); break; } } // STOP HIDING FROM INCOMPATIBLE BROWSERS --> </script> </head> <body> <form name="gradeForm"action="Your Grades"> <input type="text" name="grade" /> <input type="button" value="Check Grade" onclick="checkGrade(document.gradeForm.grade.value);" /> </form> </body> </html> What's throwing me off is the "A" in the case. As well, "(document.gradeForm.grade.value)" I need to make it a IF statement, but not sure how to call that function. Okay, so I got this code originally from another site. (A few months ago) I don't remember the site, and I was wondering if anyone could help me get it back how it was. Originally I believe the if statements were like this: Code: if (hr == 6, 7, 8) document.write("") I want the same code to appear when any of these numbers appear. Here is my current code: Code: document.write("<center><font size=+1>") day = new Date() hr = day.getMinutes() document.write("") if (hr == 0) document.write("<script type='text/javascript' src='http://www.1800banners.com/adserver/adserver.js'></script> <script type='text/javascript'> var varsarray=[]; varsarray[0]='24047'; if(!token) {var token='0'} else {var token=token+1;} var loadtype='0'; var adtype='0'; var background_color='#9C9C9C'; var border_color='#9C9C9C'; var text_color='#060606'; var text_rollcolor='#000000'; var domain_color='#000000'; varsarray['interhours']=''; varsarray['interpages']=''; adserver_initialize(token,loadtype,adtype,background_color,border_color,text_color,text_rollcolor,domain_color,varsarray); </script>") if (hr == 1) document.write("") if (hr == 2) document.write("") if (hr == 3) document.write("") if (hr == 4) document.write("") if (hr == 5) document.write(") if (hr == 6) document.write("") if (hr == 7) document.write("") if (hr == 8) document.write("") if (hr == 9) document.write("") if (hr == 10) document.write("") if (hr == 11) document.write("") if (hr == 12) document.write("") if (hr == 13) document.write("") if (hr == 14) document.write("") if (hr == 15) document.write("") if (hr == 16) document.write("") if (hr == 17) document.write("") if (hr == 18) document.write("") if (hr == 19) document.write("") if (hr == 20) document.write("") if (hr == 21) document.write("") if (hr == 22) document.write("") if (hr == 23) document.write("") if (hr == 24) document.write("") if (hr == 25) document.write("") if (hr == 26) document.write("") if (hr == 27) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 28) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 29) document.write("") if (hr == 30) document.write("<script type='text/javascript' src='http://www.1800banners.com/adserver/adserver.js'></script> <script type='text/javascript'> var varsarray=[]; varsarray[0]='24047'; if(!token) {var token='0'} else {var token=token+1;} var loadtype='0'; var adtype='0'; var background_color='#9C9C9C'; var border_color='#9C9C9C'; var text_color='#060606'; var text_rollcolor='#000000'; var domain_color='#000000'; varsarray['interhours']=''; varsarray['interpages']=''; adserver_initialize(token,loadtype,adtype,background_color,border_color,text_color,text_rollcolor,domain_color,varsarray); </script>") if (hr == 31) document.write("") if (hr == 32) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 33) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 34) document.write("") if (hr == 35) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=7&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 36) document.write("") if (hr == 37) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 38) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 39) document.write("") if (hr == 40) document.write("<script type='text/javascript' src='http://adhitzads.com/138517'></script>") if (hr == 41) document.write("") if (hr == 42) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 43) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 44) document.write("") if (hr == 45) document.write("<script type='text/javascript' src='http://adhitzads.com/136216'></script>") if (hr == 46) document.write("") if (hr == 47) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 48) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 49) document.write("") if (hr == 50) document.write("<SCRIPT LANGUAGE='JavaScript1.1' SRC='http://bdv.bidvertiser.com/BidVertiser.dbm?pid=244169&bid=856524' type='text/javascript'></SCRIPT>") if (hr == 51) document.write("") if (hr == 52) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 53) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 54) document.write("") if (hr == 55) document.write("<form action='https://www.paypal.com/cgi-bin/webscr' method='post'><input type='hidden' name='cmd' value='_s-xclick'> <input type='hidden' name='hosted_button_id' value='8220588'> <input type='image' src='https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif' border='0' name='submit' alt='PayPal - The safer, easier way to pay online!'> <img alt='' border='0' src='https://www.paypal.com/en_US/i/scr/pixel.gif' width='1' height='1'> </form>") if (hr == 56) document.write("") if (hr == 57) document.write("") if (hr == 58) document.write("") if (hr == 59) document.write("") document.write("</font></center>") Any help? Or do you need more information? Hello, I have a long string of if..else statements, and the same is used in multiple functions. How do a make a variable that I can call instead of copying 40 lines of code? Thanks! edit:: got it to work setting inBooth to false in the function where I clear the menu, but then it's refreshing to div so often while you're standing in the booth (30 frames/sec, it's a game) that nothing happens when you click the buttons to buy/sell unless you click really really fast.... anyone know a good way to check whether something is true, then toggle an innerHTML div, then once the value becomes false, toggle the innerHTML div to empty....??simply?? But if I reverse the order the opposite works but then the other doesn't...... Code: if (condition==true ) { generateSale(); inBooth=true; smithShop(); } else {inBooth=false;clearSale();} //IS PLAYER IN VENDOR BOOTH? if (condition2==true ) { generateSale(); inBooth=true; vendorShop(); } else {inBooth=false;clearSale();} Only the "generateSale()" from the second one executes. If I switch the if blocks, the second one with "generateSale()" executes. If I change "clearSale();" to a "setTimeout=('clearSale()',1000)" then both execute "generateSale()" but the div it affects flickers repeatedly as it's cleared/remade. generateSale() changes the innerHTML of a div, clearSale() just overwrites the innerHTML with ' ' Code: var sales2; function clearSale() { sales2=document.getElementById('sales2'); sales2.innerHTML=''; } function generateSale(){ sales2=document.getElementById('sales2'); sales2.innerHTML= 'Buy/Sell: '+' Gold coins: '+playerInventory[lootArray[7]]+'<br>' +' Consumables:<br>' +'You have: '+playerItems[itemArray[4]] +' arrows<br>' +'<input type="button" value="Buy" onclick="buyIt(0);">'+'10 for 50gp<br>' +'<input type="button" value="Sell" onclick="sellIt(0);">'+'10 for 25gp<br>' } Any ideas? Basically when my character enters the range of an image of a building I have a menu pop up saying you're in a vendor booth. I want it to run generateSale() which changes a div on the screen to list a bunch of stuff you can buy/sell. When your character is no longer standing in range, I want it to clear that information, so you have to be standing within range in order to trade items. Currently I can make it generates the sale menu, but it doesn't clear it when you leave the range. I've tried all kinds of "toggle" variables to set whether you're in range or not, but only the second block fires, not the first and I cannot figure out why... Any ideas? The code all works fine it just won't clear for all the different buildings, only one.... Hi I need help to finish this code with using nested if statements A price of a ticket to a passenger will be: First Class 500 Economy Class (with meal) 400 Economy Class (without meal) 200 How I can write a JavaScript code according to the following specifications: a. Read the class that the passenger wants to travel on. b. If the class is the first class, print the price of ticket. c. If the class is the economy class, ask the user if he/she wants a meal on the flight. Then print the price of the ticket according to the response of the passenger. The program should simply accept one possible strings the user enters; it is not required to work for all possible inputs such as First, first, FIRST or Yes , yes, YES. This is the code which I have been trying { var inputNum = prompt("Enter the class you want\n first class?\neconomy? :"); if (isNaN(inputNum)) { if (inputNum.match(/first class/)) { document.write("<h1><center>your Ticket is 500<\center><\h1>"); } else { prompt("<h1>if you want a meal on the flight press OK <\h1>"); document.write("<h1>your ticket is 400<h1>,"); } } Hi, I am trying to detect a users browsers and output a different code based on browser. I found this code online that uses the object detection method and it works. When I document.write(BrowserDetect.browser) it outputs Chrome, (which is what I am using). The code can be found he http://www.quirksmode.org/js/detect.html So I can't understand why the following code does not work? It ouputs the "not" in the else statement. I believe that BrowserDetect is an object and the .browser at the end is a property of the object. Can one not make an object a variable? or make an object a string? oh and BTW there is a bunch of javascript that creates the browserdetect object that can be found on the site above, but I thought it would be too unwieldy to post here. Thanks. The following script outputs: Chrome Not Working <script type="text/javascript"> document.write(BrowserDetect.browser); var browser="BrowserDetect.browser"; if(browser=="Chrome") { document.write(" Working"); } else { document.write(" Not working"); } </script> Hi, been using javascript for a while now, and wondering how else this could be coded to be more effective and to 'work'. The idea is that the user gets prompted, 'what is your birthday?' the user would put in 0121 or whatever their birthday is, as 01 = month and 21 = day. I'm sure you'll understand what I mean.... Code: <DOCTYPE html> <html> <script> var userAnswer = prompt("When is your birthday? Example: Jan 21st = 0121") </script> <head> </head> <body> <script> if(userAnswer >= 0101){ alert('You are Capricorn!') }; if(userAnswer >= 0120){ alert('You are Aquarius!') }; if(userAnswer >= 0219){ alert('You are Pisces!') }; if(userAnswer >= 0321){ alert('You are Aries') }; if(userAnswer >= 0420){ alert('You are Taurus') }; if(userAnswer >= 0521){ alert('You are Gemini!') }; if(userAnswer >= 0621){ alert('You are Cancer!') }; if(userAnswer >= 0723){ alert('You are Leo!') }; if(userAnswer >= 0823){ alert('You are Virgo!') }; if(userAnswer >= 0923){ alert('You are Libra!') }; if(userAnswer >= 1023){ alert('You are Scorpio!') }; if(userAnswer >= 1122){ alert('You are Sagittarius!') }; if(userAnswer >= 1222){ alert('You are Capricorn!') }; </script> </body> </html> Reply With Quote 01-18-2015, 10:01 AM #2 Mitchellwood101 View Profile View Forum Posts New to the CF scene Join Date Nov 2014 Posts 7 Thanks 0 Thanked 0 Times in 0 Posts And then it will alert the user of their star sign****** Reply With Quote 01-18-2015, 05:47 PM #3 Philip M View Profile View Forum Posts Supreme Master coder! Join Date Jun 2002 Location London, England Posts 18,371 Thanks 204 Thanked 2,573 Times in 2,551 Posts If you have been using Javascript for a while now, you should be aware that prompts and alerts are long obsolete, and I would advise that you should not be wasting your time learning this stuff. Use DOM methods to obtain input from and display messages to your users. Be aware that a value starting with 0 is a string, not a number, and so must be in quotes - that is why your code does not work. In any case you need rigorous checking to ensure that the birthday entered in this form is valid, e.g. not 56th Febtember or "Mickey Mouse". You would do best to obtain the input from select lists for month and day, expressed as numbers. A tiny bit more sophistication would allow the user to select a month by name, where the text of the option is the month name, and the value of that option the month number. Code: <select id = "monthlist" onchange = "getSignFunction()"> <option value = "0"> Choose your birth month</option> <option value = 1>January</option> <option value = 2>February</option> ... and so on </select> Then you want something like this:- Code: function getSignFunction()"{ var sign = ""; var month = document.getElementById("monthlist").value; var date = document.getElementById("datelist").value; if (month == 0) {return} // invalid choice if (month == 1 && date >=20 || month == 2 && date <=18) {sign = "Aquarius";} if (month == 2 && date >=19 || month == 3 && date <=20) {sign = "Pisces";} if (month == 3 && date >=21 || month == 4 && date <=19) {sign = "Aries";} if (month == 4 && date >=20 || month == 5 && date <=20) {sign = "Taurus";} if (month == 5 && date >=21 || month == 6 && date <=21) {sign = "Gemini";} if (month == 6 && date >=22 || month == 7 && date <=22) {sign = "Cancer";} if (month == 7 && date >=23 || month == 8 && date <=22) {sign = "Leo";} if (month == 8 && date >=23 || month == 9 && date <=22) {sign = "Virgo";} if (month == 9 && date >=23 || month == 10 && date <=22) {sign = "Libra";} if (month == 10 && date >=23 || month == 11 && date <=21) {sign = "Scorpio";} if (month == 11 && date >=22 || month == 12 && date <=21) {sign = "Sagittarius";} if (month == 12 && date >=22 || month == 1 && date <=19) {sign = "Capricorn";} alert (sign); // alert used here just for testing purposes. Crate as <span> and then use document.getElementById("spanid").innerHTML = sign to display the result to the user. } All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit. Give me an example for switch statements. I had to create one that tests value of parameter using a text string, I came up with this value Code: function createBar(partyType,percent){ switch(partyType) { case D: <td class='dem'></td> break; Should I use " or ' between the dem? I am using break statements. do all breaks should end with string or do i need the bracket? I hope this does not seem like i getting people to do homework for me again,, just want to verify if i did this area correctly? Trying to check validation on some dropdowns with this but man it's ugly. Is there a way to make it cleaner? Code: function checkDrop() { var sMorn = document.getElementById('wcsave_10_msession'); var sAft = document.getElementById('wcsave_10_asession'); var mMast = document.getElementById('wcsave_10_session_mmastery'); var aMast = document.getElementById('wcsave_10_session_amastery'); var mConc1 = document.getElementById('wcsave_10_session_mconc1'); var mConc2 = document.getElementById('wcsave_10_session_mconc2'); var aConc1 = document.getElementById('wcsave_10_session_aconc1'); var aConc2 = document.getElementById('wcsave_10_session_aconc2'); if(sMorn.options[sMorn.selectedIndex].value == 'Mastery session 1 - Computer Lab: Searching Essentials') { if ((sAft.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==0) && (aConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==1) && (aConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==0) && (aConc2.selectedIndex ==1)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } } if(sAft.options[sAft.selectedIndex].value == 'Mastery session 2 - Beyond the Basics of Searching!') { if ((sMorn.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==0) && (mConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==1) && (mConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==0) && (mConc2.selectedIndex ==1)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } } if(sMorn.options[sMorn.selectedIndex].value == 'Concurrent sessions 1-6') { if ((sAft.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==0) && (aConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==1) && (aConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==0) && (aConc2.selectedIndex ==1)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } } if(sAft.options[sAft.selectedIndex].value == 'Concurrent sessions 7-12') { if ((sMorn.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==0) && (mConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==1) && (mConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==0) && (mConc2.selectedIndex ==1)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } } return true; } This may be a basic programing problem but I am writing a function to validate a form using a bunch of else-if statements and encounters a problem when I use nested if/else-if statements. It will not continue on to the next else-if, after it returns false. Code: // to check each entry on the form and alert user if entry is invalid. function checkform(){ var checkssn = /(\d{3})-(\d{2})-(\d{4})/; var checkphone = /(\d{3})-(\d{4})/; var checkname = /[a-zA-Z]+(\.|,|\s)*/; var checkzip = /(^967|^968)\d{2}(-\d{4})?$/; // check to see if user have selected an Election if (!document.f1.elections[0].checked && !document.f1.elections[1].checked && !document.f1.elections[2].checked && !document.f1.elections[3].checked) { window.alert("Please select an Election!") return false; // check to see if user entered a valid SSN } else if ( checkssn.test(document.f1.ssn.value) == false){ window.alert("[1]. Please enter a valid social security number in the format ddd-dd-ddd"); return false; // check to see if user entered a valid home telephone number or business telephone number }else if ( document.f1.home_phone.value == '' && document.f1.business_phone.value == '') { window.alert("[4]. Please enter a Home or Business telephone number!") return false; } else if ( document.f1.home_phone.value != ''){ if (checkphone.test(document.f1.home_phone.value) == false){ window.alert("[4]. Please enter a valid home phone number in the format ddd-ddd"); return false; } } else if ( document.f1.business_phone.value != ''){ if ( checkphone.test(document.f1.business_phone.value) == false){ window.alert("[4]. Please enter a valid business phone number in the format ddd-ddd"); return false; } // check to see if user entered a valid Name }else if ( checkname.test(document.f1.lastname.value) == false){ window.alert("[5]. Last Name can only consist of letters, periods(.), commas(,) and spaces"); return false; }else if ( checkname.test(document.f1.firstname.value) == false){ window.alert("[5]. First Name can only consist of letters, periods(.), commas(,) and spaces"); return false; The problem occurs when it validates the phone numbers. When a valid number is entered, it will not move to the next else-if statement to validate the name. It's been years since I program in Java/C, so I'm a bit rusty. Any help is appreciated. -Alex Hey guys, here's my code Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script src="jquery-1.3.2.min.js" type="text/javascript"></script> <style type="text/css"> #box { background: red; width: 300px; height: 300px; display: none; } #box2 { background: green; width: 300px; height: 300px; display: none; } </style> <script type="text/javascript"> $(function() { $('a.about').click(function() { $('#box').stop().slideToggle(2000); }); }); $(function() { $('a.contact').click(function() { $('#box2').stop().slideToggle(2000); }); }); </script> </head> <body> <div id="box"></div> <div id="box2"></div> <a href="#" class="about" />about</a> <a href="#" class="contact" />contact</a> </body> </html> Basically I want to make it so that if you click about, and about is open, once you click contact it closes about before opening contact? I'm not sure how to approach this though whether I should use if statements or something? I'm pretty new to jquery Currently I have the following javascript switch statement ... Code: switch(firstchar){ case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": orderlist[0] += addthis; break; case "a": case "A": orderlist[1] += addthis; break; case "b": case "B": orderlist[2] += addthis; break; case "c": case "C": orderlist[3] += addthis; break; case "d": case "D": orderlist[4] += addthis; break; case "e": case "E": orderlist[5] += addthis; break; case "f": case "F": orderlist[6] += addthis; break; case "g": case "G": orderlist[7] += addthis; break; case "h": case "H": orderlist[8] += addthis; break; case "i": case "I": orderlist[9] += addthis; break; case "j": case "J": orderlist[10] += addthis; break; case "k": case "K": orderlist[11] += addthis; break; case "l": case "L": orderlist[12] += addthis; break; case "m": case "M": orderlist[13] += addthis; break; case "n": case "N": orderlist[14] += addthis; break; case "o": case "O": orderlist[15] += addthis; break; case "p": case "P": orderlist[16] += addthis; break; case "q": case "Q": orderlist[17] += addthis; break; case "r": case "R": orderlist[18] += addthis; break; case "s": case "S": orderlist[19] += addthis; break; case "t": case "T": orderlist[20] += addthis; break; case "u": case "U": orderlist[21] += addthis; break; case "v": case "V": orderlist[22] += addthis; break; case "w": case "W": orderlist[23] += addthis; break; case "x": case "X": orderlist[24] += addthis; break; case "y": case "Y": orderlist[25] += addthis; break; case "z": case "Z": orderlist[26] += addthis; break; default: alert('Illegal Entry'); break; } ... that I wish to optimize so that I do not need to write out so many lines of code. Also, I wish to take into the following as consideration ... Code: case "aa": case "Aa": case "aA": case "AA": orderlist[1] += addthis; break; ... for each two-character combination. (aa to zz) Appreciate any help. I need to create javascript that will create the ringup line (see code) based on accomodation selections. If they chose Wednesday as arrival and depart Thursday they will get the breakdown_wed_supplement value. If they arrive Wednesday and depart later than Thursday they will get charged for Wed and Thursday and finally if they arrive Thursday, they will get charge for Thursday. The Friday - Sunday values are working, but I can't get the Wed/Thurs to work. They were written by someone else and were working, but I needed to change it for the Thursday departure. I am really new to javascript and haven't got a clue where I went wrong. Code: if (getControlValue(form.accommodation)==="Yes") { if (getControlValue(form.occupancy)==="Single") { total.ringup(conferencePrices.lodgingFeeConference,'Conference lodging for single person, Friday through Sunday','breakdown_lodging'); } else if (getControlValue(form.occupancy)==="Double") { total.ringup(conferencePrices.lodgingFeeConferenceRoommate,'Conference lodging with roommate, Friday through Sunday','breakdown_lodging'); } if (form.arrivedate.selectedIndex===1 && form.departdate.selectedIndex===2) { if (getControlValue(form.roommate)==="Assign" || form.rm_arrive.selectedIndex===1 && form.rm_depart.selectedIndex===2) { total.ringup(conferencePrices.lodgingFeeNightlyRoommate,'Wednesday pre-conference lodging, with roommate','breakdown_wed_supplement'); } else { total.ringup(conferencePrices.lodgingFeeNightly,'Wednesday pre-conference lodging, single person','breakdown_wed_supplement'); } } if (form.arrivedate.selectedIndex===1 && form.departdate.selectedIndex===>2) { if (getControlValue(form.roommate)==="Assign" || form.rm_arrive.selectedIndex===1 && form.rm_depart.selectedIndex===>2) { total.ringup(conferencePrices.lodgingFeeNightlyRoommate,'Thursday pre-conference lodging, with roommate','breakdown_thurs_supplement'); total.ringup(conferencePrices.lodgingFeeNightlyRoommate,'Wednesday pre-conference lodging, with roommate','breakdown_wed_supplement'); } else { total.ringup(conferencePrices.lodgingFeeNightly,'Thursday pre-conference lodging, single person','breakdown_thurs_supplement'); total.ringup(conferencePrices.lodgingFeeNightly,'Wednesday pre-conference lodging, single person','breakdown_wed_supplement'); } } if (form.arrivedate.selectedIndex===2) { if (getControlValue(form.roommate)==="Assign" form.rm_arrive.selectedIndex===2) { total.ringup(conferencePrices.lodgingFeeNightlyRoommate,'Thursday pre-conference lodging, with roommate','breakdown_thurs_supplement'); } else { total.ringup(conferencePrices.lodgingFeeNightly,'Thursday pre-conference lodging, single person','breakdown_thurs_supplement'); } } } |