JavaScript - Multiple Function Calculations Within Onclick Switch Statements
My problem is that i have multiple math calculations within different functions that have multiple switch statements that get calculated with an onclick="functionOne()" for each radio button and before i can calculate the outcome for some reason i must fully insert a value for every input as well as check every raidio button before i get to the end of my list. I wish to simply allow the user to fill out as many fields as he wishes leaving some bank without being restricted to do every single one.. Thank you in advance
Similar TutorialsGive 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? 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. 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. Hi, I believe this question has been asked by someone else before. However, it seems that the problem wasn't resolved. I'm having some trouble with the Switch Statements. My program runs without it so I know that the problem is here. I believe the logic is sound so it may be due to incorrect syntax. I will appreciate if someone could take a look at my code below and guide me in the right direction. Thank you. The purpose of this function is to create bars, and the length of the bars is determined by the value of the "percent" variable (which is working fine) in the For loop after the Switch statements. The variable, "partyType" contains any of the text strings, such as "I", "D", etc. Code: function createBar(partyType,percent){ var barText; switch(partyType){ case "D": barText="<td class='dem'></td>"; break; case "R": barText="<td class='rep'></td>"; break; case "I": barText="<td class='ind'></td>"; break; case "G": barText="<td class='green'></td>"; break; case "L": barText="<td class='lib'></td>"; break; default: document.write("hi"); } for(var i=1; i<percent; i++){ document.write(barText); } } } Writing Table Cells and Switch Statements I am to create a function and purpose is to write blank tables cells to make up horizontal bar. function will have 2 parameters=(partyType and percent partyType parameter stores variables of D,R,I,G or L percent parameter store percentage rounded to nearest integer so I am to make a switch and break statements /commands then have to create a For Loop in counter variable goes up from 1 through value of parmeter increments of 1 heres the code I created for this one Code: function createBar(partyType,percent){ // script element to create blank cells switch(partyType) { case "D": document.write("<td class='dem'></td>"); break; case "E": document.write("<td class='rep'></td>"); break; case "I": document.write("<td class='ind'></td>"); break; case "G": document.write("<td class='green'></td>"); break; case "L": document.write("<td class='lib'></td>"); break; } var barText = partyType for (i=0; i < percent; i++) { document.write(barText); So what improvements should I make? should my loop be before everything? Thanks I know theres a lot of codes out there online for this but I cannot seem to get any of them to work for me. I am trying to make my layout for an item page for an online store. I'd like one big image, with one or 2 thumbnails under it which when clicked will switch the big image. Clicked, not mouseover. Lets say my big images would be REG1.jpg and REG2.jpg, the first being the image on the page that will switch. THUMB1.jpg and THUMB2.jpg being the thumbnails. Is there a simple way to do this or is the code a complete mess like I've been seeing on so many pages? I'd say I have an intermediate understanding of HTML and I've just started teaching myself javascript but I'm starting to fall off the wagon, I can't decipher the stuff coming up in these example codes. Any help would be thoroughly appreciated!! This is driving me crazy! So I'm using a javascript file to switch out items when a thumbnail is clicked. This includes photo, title, description, price, size, quantity and add to cart button.... The original code on the html file works fine, but as soon as it is swapped by onclick, the 'add to cart' button does nothing! Here is the code: Code: var addDisplay = document.getElementById('cart-add'); addDisplay.innerHTML = "<a class='item_add' href='/' onclick='simpleCart.add(); return false;'><img src='images/shop/cart-add.jpg' width='118' height='26' border='0'></a>"; that is what shows in my javascript file... here is what's on the html file: Code: <div id="cart-add"><a class='item_add' href='/' onclick='simpleCart.add(); return false;'><img src="images/shop/cart-add.jpg" width="118" height="26" border="0"></a> </div> and that adds the item to the cart just fine, but when a thumbnail is clicked on, activating the javascript file to switch out the item/elements, the 'add to cart' button no longer does anything... by the way, I am using simpleCart.js PLEASE HELP, PLEASE!? Please PM me if you need more info... or post here if you have a solution. Thanks! Hi, This button changes the button text when clicked, but it is always calling the same function. How to make the button depending on the button text to call different functions on onclick? [CODE] <script type = "text/javascript"> function button_switch(){ if (document.switch_form.switch_b.value == "Adam"){ document.switch_form.switch_b.value="Eva"; } else{ document.switch_form.switch_b.value = "Adam"; } } </script> <form name = "switch_form"> <input type = "button" name = "switch_b" id = "switch_b" value = "Adam" onclick = "button_switch()" /> </form> [CODE] Hello there! I am completely new to these forums and to programming in general (however, I used to program some games, albeit not very good ones, on a version of BASIC that came with my Playstation 2 about 10 years ago hehe). I have a basic understanding of functions, loops, if/else/switch and an extremely basic understanding of objects/methods. I wanted to consolidate my knowledge by putting it to practical use so I have made a little text adventure game. At the moment it is horrendously non-user-friendly as it has just been me experimenting with functions, if/else statements etc. Anyway, below is my code and I have two specific questions regarding it (but general answers telling me how I could do things more efficiently OR telling me what I should consider studying would be very appreciate also :)). Firstly, I have a little inventory object up top and when the player picks up something in the game I change the inv item to "true" so they can use it later (it seemed logical!). I would really love to know how the user could type in "inv" and for their inventory to be shown back to them. i.e. How could I show only the "true" items of an object? I have tried coding this myself but I really am stumped for an answer. Secondly, you can see in the "north21" function within the "north2" function that I used a series of if/else statements instead of a switch. I would have much prefered to use a switch and then for the case of "go north" have an if/else statement to check if vineclear is true or not (so that they can pass through to the non-existent north3 room). Is this possible? You can use if/else statements within an if/else statement but it seemed when I tried doing that in a switch it didn't like it. I am running this script through an interpreter to play the "game". I understand that JS probably isn't the best for what I am trying to do, it was more just a "see what I can do" exercise more than anything. Thanks in advance for your help and please be nice to me, forums scare me generally :D testgame.txt I have to attach the code in a txt file as for some reason it wasn't displaying properly when I copied and pasted it here. Apologies! Reply With Quote 01-17-2015, 08:26 PM #2 Old Pedant View Profile View Forum Posts Supreme Master coder! Join Date Feb 2009 Posts 28,310 Thanks 82 Thanked 4,754 Times in 4,716 Posts Briefly: Code: // based on this: var inv = { sword: false, shield: false, vines: false }; // try this: function displayInventory( ) { var list = [ ]; for ( var item in inv ) { if ( inv[item] == true ) { list.push( item ); } } if ( list.length == 0 ) { return "You have no items in your inventory"; } return "You have these items in your inventory:<ul><li>" + list.join("</li><li>") + "</ul>"; } My code is designed for display in an HTML page, not for use in clumsy console.log( ) coding. prompt() and alert() and confirm() and document.write() and console.log() should be used ONLY when debugging, not for any real work. But if you must use console.log, then try this, to replace the code in italics: Code: return "You have these items in your inventory: " + list.join(","); 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. can someone see a way to minimize this down to one statement that makes it infinate? if(x == i) || (x*1 == i) || (x*2 == i) || (x*3 == i) || (x*4 == i) || ............. x is a var user input. it is numeric. i is the counter placed above the function. I have an order form which is validated by javascript... To validate the fields I have used multiple if statements! But the problem is it works well when checking the fields from top to bottom; but if the user leaves all the upper fields blanks and only inputs the last field, the script fails to validate all the upper fields.. Please help me improve my javascript... The javascript code is validatecalculate.js Code: function setFocus() { document.getElementById("fname").focus() } function IsNumeric(strString) // check for valid numeric strings { var strValidChars = "0123456789 ";//allowed numeric and space var strChar; var blnResult = true; if (strString.length == 0) return false; // test strString consists of valid characters listed above for (i = 0; i < strString.length && blnResult == true; i++) { strChar = strString.charAt(i); if (strValidChars.indexOf(strChar) == -1) { blnResult = false; } } return blnResult; } function isNaN(strelem){ var numericExpression = /^[0-9]+$/; if(strelem.match(numericExpression)){ return true; }else{ return false; } } function validate_form() { missinginfo = ""; //alert("show hidden value " + (document.myform.totalsub.value)); if (document.getElementById("fname").value == "") { missinginfo += "\n - First Name Not Entered"; } if (document.getElementById("lname").value == "") { missinginfo += "\n - Last Name Not Entered"; } if ((document.getElementById("email").value.indexOf("@") < 1) || (document.getElementById("email").value.lastIndexOf(".") < 2)) { if (document.getElementById("email").value == "") { missinginfo += "\n - Email Address Not entered"; } else{ missinginfo += "\n - Invalid email Address.\n >>email should have the format x@abc.yy"; } } if (IsNumeric(document.getElementById("phone").value) == false) { if (document.getElementById("phone").value == "") { missinginfo += "\n - Phone Number Not entered"; } else{ missinginfo += "\n - Invalid Phone Number.\n >> Please enter numeric characters (space allowed)";} } var reg = /^\d{1,}\S\d{1,}\s[a-zA-Z]+\s[a-zA-Z]+$/; if (reg.test(document.getElementById("saddress").value)== false) { if (document.getElementById("saddress").value== "") { missinginfo += "\n - Street Address Not entered"; } else{ missinginfo += "\n - Invalid Street Address.\n >> Please use format 0/00 Tiger Street"; } } if (document.getElementById("suburb").value == "") { missinginfo += "\n - Suburb Not Entered"; } var pval = /^\d{4}$/; if (pval.test(document.getElementById("pcode").value)== false){ if (document.getElementById("pcode").value== "") { missinginfo += "\n - Postcode Not Entered"; } else{ missinginfo += "\n - Invalid Post Code.\n >> Please enter four numbers of your postcode"; } } if (document.getElementById("ccname").value == "") { missinginfo += "\n - Name on Credit Card not entered"; } cval = /^\d{16}$/; if (cval.test(document.getElementById("ccnum").value)== false){ if (document.getElementById("ccnum").value == "") { missinginfo += "\n - Credit Card Number Not entered"; } else { missinginfo += "\n - Invalid Credit Card Number.\n >> Please enter 16 digits on your credit card. Ignore the dashes and spaces."; } } ymval=/^\d{2}$/; if (ymval.test(document.getElementById("edatemonth").value)== false) { if (document.getElementById("edatemonth").value == "") { missinginfo += "\n - Credit Card Expiry Month Not Entered"; } else{ missinginfo += "\n - Invalid Expiry Month.\n >> Please enter 01 for Jan, 02 for Feb.... 11 for Nov & 12 for Dec."; } } if (ymval.test(document.getElementById("edateyear").value)== false){ if (document.getElementById("edateyear").value == "") { missinginfo += "\n - Credit Card Expiry Year Not Entered"; } else{ missinginfo += "\n - Invalid Expiry Year.\n >> Please enter last two digits only. e.g.: 12 for 2012"; } } cvvval=/^\d{3}$/; if (cvvval.test(document.getElementById("cvv").value)== false){ if (document.getElementById("cvv").value == "") { missinginfo += "\n - CVV Not Entered"; } else{ missinginfo += "\n - Invalid CVV.\n >> Please enter the 3 digits only"; } } else return true; if (missinginfo != "") { missinginfo ="______________________________________________\n" + "Form not Submitted:\n" + missinginfo + "\n______________________________________________" + "\nPlease check and submit again!"; alert(missinginfo); return false; } else return true; } function CalculateTotal() { var bolt = parseInt(document.getElementById('qnty1').value); var nut = parseInt(document.getElementById('qnty2').value); var washer = parseInt(document.getElementById('qnty3').value); document.getElementById("tot1").value = (bolt * 2.15).toFixed(2); document.getElementById("tot2").value = (nut * 0.45).toFixed(2); document.getElementById("tot3").value = (washer * 0.30).toFixed(2); document.getElementById("totqnty").value = ((bolt) + (nut) + (washer)); document.getElementById("totamount").value = ((bolt * 2.15) + (nut * 0.45) + (washer * 0.30)).toFixed(2); } The html code with the form is Code: <?xml version = "1.0" encoding = "utf-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- order.html The order page for 'Nuts 'R' Us' --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" type="text/css" href="task2.css" /> <script type="text/javascript" src="validatecalculate.js"> </script> <title>Nuts 'R' Us - Order</title> </head> <body onload="return setFocus();"> <table class="table"> <tr><td><h1> Nuts 'R' Us </h1></td></tr> <tr><td class="text2"> The best store in town.</td></tr> <tr><td> <table class="table2"> <tr><td class="td1"><a href = "main.html"> Home </a></td><td></td> <td class="td1"> <a href = "order.html">Order </a></td><td></td> <td class="td1"> <a href = "http://csusap.csu.edu.au/cgi-pub/rgauta01/process.cgi">Shopping-Cart </a></td></tr> </table></td> </tr> <tr><td> <hr /> </td></tr> <tr><td><form action = "http://csusap.csu.edu.au/cgi-pub/rgauta01/process.cgi" method="post" id="myform"> <table class="table"> <tr><td>First Name:</td> <td><input type = "text" name ="fname" id="fname" /></td></tr> <tr><td>Last Name:</td> <td><input type = "text" name ="lname" id="lname" /></td></tr> <tr><td>email:</td> <td><input type = "text" name ="email" id="email" /></td></tr> <tr><td>Phone:</td> <td><input type = "text" name ="phone" id="phone" /></td></tr> <tr><td>Street Address:</td> <td><input type = "text" name ="saddress" size ="50" id="saddress" /></td></tr> <tr><td>Suburb:</td> <td><input type = "text" name ="suburb" size ="30" id="suburb" /></td></tr> <tr><td>Postcode:</td> <td><input type = "text" name ="pcode" size ="30" id="pcode" /></td></tr><tr><td colspan="2" class="text3">Payment Details</td></tr> <tr><td colspan="2">Type of card: <input type="radio" name="cardtype" value="visa" id="visa" checked="checked" />Visa <input type="radio" name="cardtype" value="mastercard" id="mastercard" />Mastercard <input type="radio" name="cardtype" value="amex" id="amex" />Amex <input type="radio" name="cardtype" value="diners" id="diners" />Diners</td></tr> <tr><td>Name on Credit Card:</td> <td><input type = "text" name ="ccname" size ="50" id="ccname" /></td></tr> <tr><td>Credit Card Number</td> <td><input type = "text" name ="ccnum" maxlength ="16" size ="50" id="ccnum" /></td></tr> <tr><td colspan="2" class="text3">Expiry Date</td></tr> <tr><td>Month(MM):</td> <td><input type = "text" name ="edatemonth" size ="5" maxlength ="2" id="edatemonth" /></td></tr> <tr><td>Year(YY):</td> <td><input type = "text" name ="edateyear" size ="5" maxlength ="2" id="edateyear" /></td></tr> <tr><td>CVV:</td> <td><input type = "text" name ="cvv" size ="5" maxlength ="3" id="cvv" /></td></tr> <tr><td colspan="2" class="text3">Order Details</td></tr> <tr><td colspan="2"><table class="table3"> <tr><th>Item</th> <th>Product Code</th> <th>Diameter</th> <th>Length</th> <th>Colour</th> <th>Unit</th> <th>Quantity</th> <th>Price($)</th></tr> <tr><td>Bolt</td><td>B113</td> <td>9</td><td>50</td> <td>Black</td> <td>2.15</td> <td class="td1"><input type ="text" name ="qnty1" size="3" onchange="CalculateTotal()" id="qnty1" value="0" /></td> <td class="td1"><input type ="text" name ="tot1" size ="3" id="tot1" value="0.00" readonly="readonly" /></td></tr> <tr><td>Nut</td> <td>N234</td> <td>5</td> <td>N/A</td> <td>Silver</td> <td>0.45</td> <td class="td1"><input type ="text" name ="qnty2" size ="3" onchange="CalculateTotal()" id="qnty2" value="0" /></td> <td class="td1"><input type ="text" name ="tot2" size ="3" id="tot2" value="0.00" readonly="readonly" /></td></tr> <tr><td>Washer</td> <td>W359</td> <td>8</td> <td>N/A</td> <td>Silver</td> <td>0.30</td> <td class="td1"><input type ="text" name="qnty3" size ="3" onchange="CalculateTotal()" id="qnty3" value="0" /></td> <td class="td1"><input type ="text" name ="tot3" size ="3" id="tot3" value="0.00" readonly="readonly" /></td></tr> <tr><td colspan = "6" class="td2">Total: </td><td class="td1"><input type ="text" name ="totqnty" size ="3" id="totqnty" value="0" readonly="readonly" /></td> <td class="td1"><input type ="text" name ="totamount" size ="3" id="totamount" value="0.00" readonly="readonly" /></td></tr> </table></td></tr> <tr><td colspan="2" align="center"><input type = "reset" value = "Reset" /> <input type = "submit" value = "Submit" name="submit" onclick="return validate_form();"/></td></tr></table> </form> </td> </tr> <tr><td colspan="2"> <a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a> </td></tr> <tr><td class="td1"> <hr /> © Nuts 'R' Us - 2011. All Rights Reserved. </td> </tr> </table></body> </html> 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'); } } } I'm currently working on a project and I am doing a bunch of image switching. I'm having a problem with the following... I have seven medium image objects and one small one. One is at the top and the other 7 are below. When one of the 7 is clicked, it then becomes the one up top and the one up top then takes the place of the image clicked. This needs to be able to happen no matter which of the seven i click. Also when you click one of the seven it runs a script to change 9 other images in the center of the page. this isnt too important because i have it working already. What i have is, each of the seven images run their function that changes the 9 center images and then it runs another function. What i need is for that function to determine which company for example(shaws, lowes, target) the top image belongs to and replace the image that was clicked with the top one. But i also need to replace the NAME="" and ONCLICK="function()" with the proper ones for the original company up top. Please if you can understand what im trying to do let me know, if you need further clarification i can do so. i can draw a picture of what im trying to do or the layout if needed but i cant necessarily show anyone the project due to a non-disclosure. My switch function just goes to the default. I've double double checked the file paths of the image on the page and it's right. So I'm not sure what's going on. here's what I got: Code: function change2(picName,imgName) { switch (document[picName].src) { case document[picName].src= "images/leaf_shapes/leaf_auriculate.gif": document[picName].src = "images/leaf_shapes/crenate/leaf_elliptic_crenate.gif"; break; case document[picName].src= "images/leaf_shapes/leaf_cordate.gif": document[picName].src= "images/leaf_shapes/serrate/leaf_elliptic_serrate.gif"; break; default: document[picName].src= "images/leaf_shapes/transparent1.gif"; } } Hi there, I've modified some code I found with a single variable & function to not allow certain keyboard input from a form (I eventually want this info to go into a cookie). I made two variables & functions; one which would not allow text, and one which would not allow numbers. The (new) text variable/function works fine, but the (modified) number variable/function only disallows the numbers 8 & 9. Please look at my code, try it (it doesn't work in IE8 or Firefox), and let me know how to fix it. Thanks, mitchellA PS: this is my first posting and I'm not sure I've followed the rules properly. Code: <html> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Character Removal Test - by mitchellA</title> <script language="javascript" type="text/javascript"> /* Visit http://www.yaldex.com/ for full source code and get more free JavaScript, CSS and DHTML scripts! */ /* This script has been heavily modified from the original by mitchellA. */ <!-- Begin var only_t = /[$\\@\\\#\%\^\&\*\(\)\[\]\+\ \{\}\`\~\=\|\/\?\.\,\<\>\'\"\!\:\;\0\1\2\3\4\5\6\7\8\9]/; var only_n = /[$\\@\\\#\%\^\&\*\(\)\[\]\+\ \{\}\`\~\=\|\/\?\.\,\<\>\'\"\!\:\;\a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z]/; function chk_t(val) { var strPass = val.value; var strLength = strPass.length; var lchar_1 = val.value.charAt((strLength) - 1); if(lchar_1.search(only_t) != -1) { var tst = val.value.substring(0, (strLength) - 1); val.value = tst; } } function chk_n(val) { var strPass = val.value; var strLength = strPass.length; var lchar_2 = val.value.charAt((strLength) - 1); if(lchar_2.search(only_n) != -1) { var tst = val.value.substring(0, (strLength) - 1); val.value = tst; } } function chk2(form) { if(form.value.length < 1) { alert("Missing something."); return false; } if((form.value.search(only_t) == -1) ||(form.value.search(only_n) == -1)) { alert("Seems to be working."); return false; } } // End --> </script> </head> <form name=get_info onSubmit="return chk2(this.f_name); return chk2(this.l_name); return chk2(this.exp_hrs);"> <br> <br> First Name: <input type="text" name="f_name" size="25" maxlength="15" value="" onKeyUp="javascript:chk_t(get_info.f_name);"> <br> <br> Last Name: <input type="text" name="l_name" size="25" maxlength="20" value="" onKeyUp="javascript:chk_t(get_info.l_name);"> <br> <br> How many minutes do you want your cookie to last (10 - 60): <input type="text" name="exp_mins" size="3" maxlength="2" value="" onKeyUp="javascript:chk_n(get_info.exp_mins);"> <br> <br> <input type=submit value=Continue> <br> </form> <body> </body> </html> i have an image with an onclick function and parameters 0,1 i want to change the parameters of the onclick function through the same function? Code: <img id="down0" onclick=swap(0,1) /> <javascript> function swap(a,b){ var imgda=document.getElementById('down'+a); imgda.onclick = swap(b,b+1); } </javascript> normally it should change the onclick to swap(1,2) but it remains 0,1 whats happening?? p.s. plz dont tell me about the javascript tag, its just for quick explaining... Good day all: I'm wondering if the cancellation of one of my onclick js function in the below code is as a result of having multiple onclick function. here is the code: Code: <FORM NAME="SubCategory" method=post action='accountingsearch2.php' rel="nofollow" target='_top'> <p align="left"> <div id="presetlabel">Or use Preset Periods-->></div> <div id="SubCategory"> <SELECT NAME="specifiedperiod" style="display:inline";> <OPTION>Select a Time Period <OPTION>Today <OPTION>Current Week <OPTION>Current Month <OPTION>Current Quarter <OPTION>Past Quarter <OPTION>Current Year <OPTION>Previous Year </SELECT> </div> <div id="proceed2"><input type='submit' value='Proceed!' onclick='return ValidateSubCategory();'></div><!--the validation does not work with onsubmit--> </FORM> <form name="SubCategory2" method=post action='accountingsearch.php' rel="nofollow" target='_top'> <div id="callabel"><label for="theDate">From today thru</label></div> <div id="othersearch"><input type="text" value="2010/10/24" readonly name="theDate" size="11"></div> <div id="cal"><input type="button" value="Calendar" onclick="displayCalendar(document.forms[0].theDate,'yyyy/mm/dd',this)" style="display:inline";></div> <!--<INPUT TYPE="" VALUE="" name="specifiedperiod" style="display:inline";--> <!--<font size='2'> Please Specify an optional Device name </font><input id='searchField' name='searchField' value='(none)' type='text'></option>--> <div id="proceed"><input type='submit' value='Proceed!'><!--onclick='return ValidateSubCategory();'--> </div> </p> </FORM> If such is the case, how can I address this problem? Currently, the first onclick function Code: onclick='return ValidateSubCategory();' works fine. The problem is with the second Code: onclick="displayCalendar(document.forms[0].theDate,'yyyy/mm/dd',this)" style="display:inline"; Any assistance is greatly appreciated! Mossa Ok, so basically what I've got is a button which when clicked, "opens" a section of the page. I also need the button to "jump" to this section of the page, because it's below the current content and won't be visible. What I've got so far is ... Javascript <script language="javascript"> function toggle() { var ele = document.getElementById("toggleText"); var text = document.getElementById("displayText"); if(ele.style.display == "block") { ele.style.display = "none"; text.innerHTML = "show"; } else { ele.style.display = "block"; text.innerHTML = "hide"; location="#ordernow" } } function moveWindow(){window.location.hash="ordernow"} </script> HTML ... (The rest of the form is up here) <input name="Button" type="button" class="button" value="Order Now" onclick="toggle();moveWindow()" id="displayText"/> ... (This is the start of the section that becomes visible, along with the anchor point which is highlighted in red.) <div id="toggleText" style="display: none"> <a name="ordernow"></a> <fieldset> <legend>Order Confirmation</legend> <div class="standardinput"> <label>Company:</label> <input name="Company" type="text" class="textinput" id="Company" /> </div> If I put two separate buttons, both functions will work. Basically I need them to be on the same function, not sure how to do that? Thanks so much in advance =) |