JavaScript - Creating Code With If...else If...else Statement
Hi, I am new here without any coding skills but willing to learn. I know how
to use html code. I am thinking of how to write the code for below scenario to create a simple online customize calculator: There is 1 box which allow us to enter any number=x (representing amount of money). So whenever we entered a number in the box and click "CALCULATE" buton below the box, there will be 3 results generated in 3 boxes below it based on the set of of rules i.e. 1. if the amount entered is <21,000 Result 1 = 1.5%*x*12 Result 2 = 1.5%*x*48 Result 3 = 1.5%*x*120 2. if the amount entered is >=21,000 and <210,000 Result 1 = 1.8%*x*12 Result 2 = 1.8%*x*48 Result 3 = 1.8%*x*120 3. if the amount entered is >=210,000 Result 1 = 2.2%*x*12 Result 2 = 2.2%*x*48 Result 3 = 2.2%*x*120 I understand that this code will involve If...else if...else Statement.. Anyone can give me any references/examples similar to this scenario? Thanks. Similar TutorialsIm creating a switch statement for this case, but dont know how to start - Create a Switch statement that tests the value of the partyType parameter. If partyType equals "D", store the following text string in a variable named barText: Code: <td class='dem'> </td> If partyType equals "R", barText should equal Code: <td class='rep'> </td> etc.. So since its going to test the value of the partyType parameter, and store the text string in a variable named barText do i begin like: Code: var barText switch (partyType) and how do I write the equals "D", etc. the only examples I have are Code: if (seating = = 'B') doc.write ("Your seat is in the balcony") ^ would i write it like that except seating would = partyType and "Your seat in the balcony" would equal the "<td class etc." ie Code: if (partyType = = "D") doc.write ("<td class='dem'> </td>") I'm trying to get my Client Side Firefox DHTML app to display a list of eBooks. For this, i have the following files F:\Textbooks.html F:\eBooks.txt F:\FirstBook.txt F:\SecondBook.txt F:\ThirdBook.txt textbooks.html is my DHTML app eBooks.txt is the Library file with a listing of all of my eBooks. Inside of eBooks.txt is the following data: ----------------- FirstBook.txt, SecondBook.txt, ThirdBook.txt, ----------------- FirstBook.txt to ThirdBook.txt are my actual ebooks. The problem that i'm having is that When i try to click on any buttons other than the FirstBook button, i get the following error: ---------------------------------- Error: unterminated string literal Source File: file:///F:/Textbooks.html Line: 1, Column: 10 Source Code: LoadEbook(' ---------------------------------- So, unlike clicking on the FirstBook button, these other buttons do not load the eBook data into the DIV for displaying the eBook data. I use the DOM insepector to checkout the DOM of the button code, and it seems like whitespace maybe is the problem. However, i have removed whitespace from the HTMLdata string, and that's not fixing the problem. did i forget something silly? LOL i'm using FireFox 3.5 to develop this App. So obviously this will not work with anything other than Gecko Based browsers. here is my HTML code: <html> <head> <script language="JavaScript"> var eBookLibrary = "eBooks.txt"; var SystemPath = "f:" + String.fromCharCode(92) function Init() { // Initialize the eBook reader document.getElementById("EbookCanvas").style.visibility = "hidden"; document.getElementById("EbookToolbar").style.visibility = "visible"; document.getElementById("FileManager").style.visibility = "visible"; // Load the List of eBooks in the Library LoadBookList(); } function UpdateEbookList() { // Update the Library of Ebooks alert("Updating eBook Library"); // Go back to the File Manager, and Reload the List of Ebooks LoadBookList(); } function LoadBookList() { // This will load the list of books that are available var EbookList = LoadFromDisk(SystemPath + eBookLibrary); var EbookListArray = EbookList.split(","); for(var x = 0; x < EbookListArray.length -1; x++) { // Strip the Filename Extension off of the eBook File Name // The Name of the Book is always the first Index in the Array var BookName = EbookListArray[x].split("."); // Remove the weird whitespace - it screws things up...i think... BookName[0] = BookName[0].replace(/(^\s*|\s*$)/g, ""); var HTMLdata = HTMLdata + "<input type='button' value='" + "FirstBook" + "'" + " onClick=LoadEbook('" + EbookListArray[x] + "');><br>"; } // For some ****ed up reason the first string always generates an 'undefined' even though it's nonsense // So just delete that from the HTMLdata string, because it's just ugly - LOL HTMLdata = HTMLdata.replace("undefined", ""); HTMLdata = HTMLdata.replace("", " "); // Write the HTML data to the DIV document.getElementById("FileManager").innerHTML = HTMLdata; } function LoadEbook(EbookName) { // Hide the File Manager and Show the Ebook Canvas document.getElementById("FileManager").style.visibility = "hidden"; document.getElementById("EbookCanvas").style.visibility = "visible"; document.getElementById("EbookToolbar").style.visibility = "visible"; // Load the Ebook content into the Ebook Reader Pannel var EbookContent = LoadFromDisk(SystemPath + EbookName); document.getElementById("EbookCanvas").innerHTML = EbookContent; } function LoadFromDisk(filePath) { if(window.Components) try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); file.initWithPath(filePath); if (!file.exists()) return(null); var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream); inputStream.init(file, 0x01, 00004, null); var sInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream); sInputStream.init(inputStream); return(sInputStream.read(sInputStream.available())); } catch(e) { //alert("Exception while attempting to load\n\n" + e); return(false); } return(null); } </script> </head> <body onLoad="Init();"> <div id="FileManager" style="position: absolute; top: 0px; left: 0px; visibility: visible;"> The eBook Library's List of Books will be listed here. Click on one to open it in the eBook Reader </div> <br> <div id="EbookCanvas" style="position: absolute; top: 0px; left: 0px; visibility: hidden;"> </div> <br> <div id="EbookToolbar" style="position: absolute; top: 100px; left: 0px;"> <input type="button" value="Open" OnClick="Init();"> <input type="button" value="Update" OnClick="UpdateEbookList();"> <input type="button" value="Exit" OnClick="MainMenu();"> </div> </body> </html> Hi everyone, I'm a beginner who has now come across the switch statement and has been trying to understand it with this simple coding i came up with. I think I have the syntex of the switch command correct but I'm trying to get it to work along with a HTML form and a function. I've been trying to figure out what i am do wrng but can not seem to see the solution. can someone guide me to the solution? thanks John Code: <html> <head> <title>Using the switch statement.</title> <script type="text/javascript"> function basegraincheck(){ var basegrain = document.basegrainform.basegrain1.value return basegrain } switch (basegrain) { case 1 : displaygrain = "US Pale ale malt" break case 2 : displaygrain = "Maris Otter Malt" break case 3 : displaygrain = "Crystal Malt" break default: displaygrain = "somethings wrong with the switch statement" } document.write(displaygrain); </script> </head> <body> <form name="basegrainform" action="" method="get" onsubmit="basegraincheck()"> <h2>Select a grain</h2> <br> <p>Pale ale Malt</p> <input type="radio" name="basegrain1" value="1"> <br> <p>Maris Otter Malt</p> <input type="radio" name="basegrain1" value="2"> <br> <p>Crystal Malt</p> <input type="radio" name="basegrain1" value="3"> <br> <input type="submit" value="Submit"> </form> </body> </html> So when I enter in numbers for the calculator the numerical grade shows, but not the letter grade. I dont know if I called the function wrong or if my if statement is wrong. I have no idea. Can someone help me please? Code: <!DOCTYPE html> <html lang="en"> <head> <title>Lab</title> <meta charset="utf-8"> <script> function calculateGrade () { var test1 = document.getElementById("t1") var test2 = document.getElementById("t2") var finalexam = document.getElementById("t3") var labs = document.getElementById("t4") var projects = document.getElementById("t5") var quizzes = document.getElementById("t6") var grade= document.getElementById("fng") grade.value=parseFloat(0.15*test1.value)+parseFloat(0.15*test2.value)+parseFloat(0.20*finalexam.value)+parseFloat(0.30*labs.value)+ parseFloat(0.05*projects.value)+parseFloat(0.15*quizzes.value) } function getLetterGrade() { if (totalGrade >= 90) letterGrade = "A"; else if (totalGrade < 90 && totalGrade >= 80) letterGrade = "B"; else if (totalGrade < 80 && totalGrade >= 70) letterGrade = "C"; else if (totalGrade < 70 && totalGrade >= 60) letterGrade = "D"; else if (totalGrade < 60 && totalGrade >= 50) letterGrade = "F"; } </script> </head> <body> <form id="form1" name="form1"> <table> <tr> <th colspan="2"> Score </th> </tr> <tr> <td> Test 1 </td> <td><input type="text" name="t1" id="t1" value= ""></td> </tr> <tr> <td> Test 2 </td> <td><input type="text" name="t2" id="t2" value= ""></td> </tr> <tr> <td> Final Exam </td> <td><input type="text" name="t3" id="t3" value=""></td> </tr> <tr> <td> Labs </td> <td><input type="text" name="t4" id="t4" value=""></td> </tr> <tr> <td> Projects </td> <td><input type="text" name="t5" id="t5" value=""></td> </tr> <tr> <td> Quizzes </td> <td><input type="text" name="t6" id="t6" value=""></td> </tr> <tr> <th colspan="2"> <input type="button" name="b1" id="b1" value="Calculate Grade" onclick="calculateGrade(); getLetterGrade()"> </th> </tr> <tr> <td> Final numerical grade </td> <td><input type="text" name="fng" id="fng" style="border: none"></td> </tr> <tr> <td> Final letter grade </td> <td><input type="text" name="letterGrade" id="letterGrade" style="border: none"></td> </tr> </table> </form> </body> </html> Hello! I am trying to create a javascript code in which I prompt the user for a number (an integer from 0 to 100) to search for, then search a function with the number they entered and then display whether the number was found, and if found, a location where it can be found within the list. Can you please tell me what is wrong? <html> <head> </head> <body> <script language = "javascript"> var list = new Array(200); for (var i=0; i < 200; i++){ list=Math.round(Math.random()*100) + " "; document.write(list); } var value=prompt("What is the number you are searching for? ", " "); if (list[i]==value){ {alert("Number has been found in location" +i)}; } </script> </body> </html> Thank you Hello everyone, I am using javascript and I have a radio button that the user selects and I have a function to see which button was selected, but i'm trying to use the return of that if statement in another statement. Basically another part of the function is dependent on what the user selects in the radio button. Here is what I have so far (I have some things in there that might not work because i'm trying to figure out what works): Code: <script type="text/javascript"> function getSelectedRadio() { len = document.dates.dep.length // returns the array number of the selected radio button or -1 if no button is selected if (document.dates.dep[0]) { // if the button group is an array (one button is not an array) for (var i=0; i<len; i++) { if (document.dates.dep[i].checked) { return i } } } else { if (document.dates.dep.checked) { return 0; } // if the one button is checked, return zero } // if we get to this point, no radio button is selected return -1; } function Calculate() { var i = getSelectedRadio(); if (i == -1) { alert("Please select if Marine entered Delayed Entry Program"); } else { if (document.dates.dep[i]) { return document.dates.dep[i].value; } else { return document.dates.dep.value; } } if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && document.dates.dayDEAF.value < 01 && first return from above ) { document.dates.yearDEP.value = (document.dates.yearDEAF.value); document.dates.monthDEP.value = (document.dates.monthDEAF.value); document.dates.dayDEP.value = (document.dates.dayDEAF.value); document.dates.yearPEBD.value = (document.dates.yearDEAF.value); document.dates.monthPEBD.value = (document.dates.monthDEAF.value); document.dates.dayPEBD.value = (document.dates.dayDEAF.value); } else if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && document.dates.dayDEAF.value < 01 && second return from above ) { document.dates.yearPEBD.value = (document.dates.yearAFADBD.value); document.dates.monthPEBD.value = (document.dates.monthAFADBD.value); document.dates.dayPEBD.value = (document.dates.dayAFADBD.value); document.dates.yearDEP.value = "N/A"; document.dates.monthDEP.value = "N/A"; document.dates.dayDEP.value = "N/A"; } } </script> I color coded in red the returns i'm trying to reference and where they need to be. Is this possible, and if so how can I do it? I haven't been able to find anything on the internet so far. Any help is greatly appreciated! This post will contain a few guidelines for what you can do to get better help from us. Let's start with the obvious ones: - Use regular language. A spelling mistake or two isn't anything I'd complain about, but 1337-speak, all-lower-case-with-no-punctuation or huge amounts of run-in text in a single paragraph doesn't make it easier for us to help you. - Be verbose. We can't look in our crystal bowl and see the problem you have, so describe it in as much detail as possible. - Cut-and-paste the problem code. Don't retype it into the post, do a cut-and-paste of the actual production code. It's hard to debug code if we can't see it, and this way you make sure any spelling errors or such are caught and no new ones are introduced. - Post code within code tags, like this [code]your code here[/code]. This will display like so: Code: alert("This is some JavaScript code!") - Please, post the relevant code. If the code is large and complex, give us a link so we can see it in action, and just post snippets of it on the boards. - If the code is on an intranet or otherwise is not openly accessible, put it somewhere where we can access it. - Tell us any error messages from the JavaScript console in Firefox or Opera. (If you haven't tested it in those browsers, please do!) - If the code has both HTML/XML and JavaScript components, please show us both and not just part of it. - If the code has frames, iframes, objects, embeds, popups, XMLHttpRequest or similar components, tell us if you are trying it locally or from a server, and if the code is on the same or different servers. - We don't want to see the server side code in the form of PHP, PERL, ASP, JSP, ColdFusion or any other server side format. Show us the same code you send the browser. That is, show us the generated code, after the server has done it's thing. Generally, this is the code you see on a view-source in the browser, and specifically NOT the .php or .asp (or whatever) source code. I'm not very familiar with java script at all, but could someone please explain how to make an if else else statement that follows these guidelines? 1. If the income is $70,000 or above the tax rate starts at 70% (the % sign will not show) 2. If the income is $20,000 or below the tax rate starts at 10% (the % sign will not show) 3. Otherwise the tax rate starts at 25% Thank you SO much! Hello, I have just started studying JS and don't know a whole lot about it. One exercise I have been given is to use the while statement to ask the user to guess a number between one and ten. this is the code I have attempted so far but it does not work. Simple for any one who is good at this. Impossible to me. Any takershttp://www.codingforums.com/images/smilies/confused.gif <!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>While Test</title> <script type="text/javascript"> <!-- function whileTest(){ var number = 4; var answer = 0; while(number != answer ){ answer=prompt("Guesss a number between 1 and ten",""); } alert("4 is the correct answer") //--> </script> </head> <body onLoad="whileTest();"> </body> </html> Hello CF im wondering how could i use an if statement to have something pop up when visitors visit my site that allows them two choices. it would say something like this. Would you Like to always see the navigation Bar? then they have two options. Yes and No. then depending on what they choose it will either have the code use the absolute or the fixed positioning attribute for the whole code. Or is this just not possible? If you want me to explain it more just ask. Code: <script type="text/javascript"> var clicks=0; function linkClick(){ document.getElementById('clicked').value = ++clicks; if (clicks>2) { var answer = confirm ("What choice would you like?") if (answer) window.open("choiceA.html",'','scrollbars=no,menubar=no,height=600,width=800,resizable=no,toolbar=no,location=no,status=no') else (answer) window.open("choiceB.html",'','scrollbars=no,menubar=no,height=450,width=450,resizable=no,toolbar=no,location=no,status=no') } } </script> OR Code: <script type="text/javascript"> var clicks=0; function linkClick(){ document.getElementById('clicked').value = ++clicks; if (clicks>2) { var answer = confirm ("What choice would you like?") if (answer){ window.open("choiceA.html",'','scrollbars=no,menubar=no,height=600,width=800,resizable=no,toolbar=no,location=no,status=no') } else (answer) window.open("choiceB.html",'','scrollbars=no,menubar=no,height=450,width=450,resizable=no,toolbar=no,location=no,status=no') } } </script> OK, so now I'm trying so open a specific page depending on the users answer. I am aware that my code should look like this: Code: if (condition) { code to be executed if condition is true } else { code to be executed if condition is not true } However because of my click counter I have been unable to do this. My code itself does kinda work BUT choiceA open both choiceA and choiceB's pages :-( choiceB is fine and opens choiceB's page. I know that it's probably a small syntax error but I just can't see where. Thank-you in advance for your assistance. can someone help me with this pls: Code: function anything() { var tegenover = document.getElementById("minlas"); var septo = document.getElementById("combo_2"); //prijs verbinder per meter var serr = document.getElementById("comba_2"); //prijs verbinder per meter var seprr = document.getElementById("combb_2"); //prijs verbinder per meter var septrr = document.getElementById("combc_2"); //prijs verbinder per meter var nocho = document.getElementById("wide"); //breedte band in mm document.getElementById("totprijslas").value = Math.round(100*((parseFloat(septo.value) + parseFloat(serr.value) + parseFloat(seprr.value) + parseFloat(septrr.value)) * parseFloat(nocho.value))/1000)/100; } the value returned of totprijslas can be anything between 0 and 1000,but i want the value of "totprijslas" to be a minimum of "50",so for example if result is 30 then i need it to show 50.can someone help me with an if statement for this.thx I am relatively new to JavaScript coding, and I a having some problems with an IF statement. I want the IF statement to tell me when the number is below the minimum. Code: function main (txtpamphletMin,txtpamphletMax,optQuality,selIncrement,txtResults) { var txtpamphletMin,txtpamphletMax,optQuality,selIncrement; var amount = 100; var errorMsg = ""; var results = ""; pamphletMin = pamphletMin.value; //Make sure that the minimum number of pamphlets required is entered if (pamphletMin == "pamphletMin<100") { errorMsg = "Number of pamphlets must be above 100"; } else { pamphletMin = txtpamphletMin.value; //get the number of pamphlets from the text box errorMsg = validatepamphletMin(Minimum Pamphlets); } I know that this is no the entire statement but I am just having problems with this one. Can anyone help? Hi! I am a total newbie I'm afraid but I really enjoy learning javascript. Today I'm trying to write an if statement. I want the image1 on a web page to change when it's clicked to image2. Then I want the if statement to see if the source of the image is the changed image2.jpg (or still the original image1.jpg) and if it is the changed one I want another click to change it to image3. Here's my code, but the change to image 3 is not working: /* image change start */ var imgchanger = document.getElementById("changingimage"); imgchanger.onclick = function() { imgchanger.src = "image2.jpg"; }; /* image change 2 start */ if (imgchanger.src == "image2.jpg") { imgchanger.onclick = function() { imgchanger.src = "image3.jpg"; }; } No matter what first name and last name I enter I get "Wrong!" I get wrong if I enter john smith or bill shaw Code: <html> <head> </head> <body> <script type="text/javascript"> var input; var first; var last; input = window.prompt("Please Enter First and Last Name (e.g. Joe Smith)"); if ((first == "John" || first == "john") && (last == "Smith"|| last == "smith")){ document.write ("Welcome John Smith"); } if ((first == "Bill" || first == "bill") && (last == "Shaw"|| last == "Shaw")){ document.write ("Welcome Bill Shaw"); } else { document.write("Wrong!"); } </script> <body> </html> Code: function showResults( results ) { if ( results.length == 0 ) { msg = "no projects found"; } else {msg = results [0]; } document.getElementById("RESULTS").innerHTML = msg; //= results.join( "<br/>" ); cannot get it to show "no projects found" for 0 results and also to show all multiple results if multiple are found. works right now for 1 result but not multiple. the =results.join( "<br/>" ); works if multiple but then it will not say no projects found for 0 results. I want to know how I can update my power_points field if a select option is chosen. For example: if animal guardian is chosen Power_points - (attribute_level * 1) if combined attacks is chosen power_points - (attribute_level * 1) if elemental control is chosen power_points - (attribute_level * 3) etc... Here is my current script: Code: <?php session_start(); ?> <form> <table cellpadding="3" cellspacing="3"> <tr> <th>Attribute</th> <th>Level</th> <th>Power Points:</th> <th>Character Points:</th> </tr> <tr> <td><input type="text" readonly="readonly" value="Scout/Knight Powers" size="19" name="attribute1" /></td> <td><select name="level" onclick="power(), remain()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select></td> <td align="center"><input type="text" name="power_points" id="power_points" size="1" readonly="readonly"/></td> <td align="center"><input type="text" name="cp_remain" id="cp_remain" size="1" readonly="readonly" /></td> </tr> </table> </form> <form method="POST" action="test_batosai.php"> <table><tr><td> <div id="dynamicInput1"> <table cellpadding="3" cellspacing="3"> <thead> <tr> <th>Sub-Attribute</th> <th>Level</th> <th>Notes <i>(optional)</i></th> </tr> </thead> <tbody> <tr> <td> <select name="attribute[]" size="1"> <option value="1">Animal Guardian</option> <option value="2">Combined Attacks</option> <option value="3">Elemental Control</option> <option value="4">Emotional Control</option> <option value="5">Item Of Power</option> <option value="6">Knight Attack</option> <option value="7">Rejuvenation</option> <option value="8">Sailor Scout Attack</option> </select></td> <td> <select name="attribute_level[]"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select></td> <td><textarea name="sub_notes[]" rows="1" cols="30"> </textarea></td> </tr> </tbody> </table> </div> <div id="cloneDiv1"> </div> </td> <td> <div id="dynamicInput2"> <table cellpadding="3" cellspacing="3"> <thead> <tr> <th>Neutral Attribute</th> <th>Level</th> <th>Notes <i>(optional)</i></th> </tr> </thead> <tbody> <tr> <td> <select name="attribute2[]" size="1"> <option value="1">Acrobatics</option> <option value="2">Appearance</option> <option value="3">Art Of Distraction</option> <option value="4">Combat Mastery</option> <option value="5">Damn Healthy!</option> <option value="6">Divine Relationship</option> <option value="7">Energy Bonus</option> <option value="8">Extra Attacks</option> <option value="9">Focused Combat</option> <option value="10">Fortified Body</option> <option value="11">Heightened Negaverse Power</option> <option value="12">Heightened Senses</option> <option value="13">Heightened Scout Powers</option> <option value="14">Massive Damage</option> <option value="15">Powerful Mind</option> <option value="16">Special Attack/Defense</option> <option value="17">Speed</option> <option value="18">Strong Soul</option> <option value="19">Supernatural Training</option> <option value="20">Unique Character Attribute</option> </select></td> <td> <select name="attribute_level2[]"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select></td> <td><textarea name="neutral_notes[]" rows="1" cols="30"> </textarea></td> </tr> </tbody> </table> </div> <div id="cloneDiv2"> </div> </td></tr></table> <input type="button" value="Add another Sub-Attribute" onClick="addInput('dynamicInput1','cloneDiv1');"> <input type="button" value="Add another Neutral Attribute" onClick="addInput('dynamicInput2','cloneDiv2');"> <input type="submit" /><input type="reset" /> </form> <script type="text/javascript"> var counter = 1; var limit = 10000000;// Set a limit if you want to function addInput(divFrom,divTo){ if (counter == limit) { alert("You have reached the limit of adding " + counter + " inputs"); } else { var newdiv = document.createElement('div'); newdiv.innerHTML = document.getElementById(divFrom).innerHTML; document.getElementById(divTo).appendChild(newdiv); counter++; } } //function for Power Points function power() { var first,res1; //Take the value of first textbox and convert it to float first=parseFloat(document.forms[0].level.value); res1=(first)*10; //show the result in the result textbox document.forms[0].power_points.value=res1; } //end function for Power Points //function for Character Points remaining function remain() { var first,res2; //Take the value of first textbox and convert it to float first=parseFloat(document.forms[0].level.value); res2=10-((first)*4); //show the result in the result textbox document.forms[0].cp_remain.value=res2; } //end function for Character Points </script> Can anyone help? I've got this code below to change an images source url based on the clicks from a dropdown menu. Is it possible to shorten this with some sort of if/or statement instead of all the else if's? something that says "if crenate or serrate or this or that, do this" here's the function: Code: function change3(picName,choice) { var url = (document[picName].src); if (/crenate\D{1}/.test(url)) { url = url.replace(/crenate/, choice) document[picName].src=(url); } else if (/margin_finelyserrate\D{1}/.test(url)) { url = url.replace(/margin_finelyserrate/, choice) document[picName].src=(url); } else if (/margin_lobed\D{1}/.test(url)) { url = url.replace(/margin_lobed/, choice) document[picName].src=(url); } else if (/margin_undulate\D{1}/.test(url)) { url = url.replace(/margin_undulate/, choice) document[picName].src=(url); } else if (/doubleserrate\D{1}/.test(url)) { url = url.replace(/doubleserrate/, choice) document[picName].src=(url); } else if (/leaf_shapes\D{1}/.test(url)) { url = url.replace(/leaf_shapes/,"leaf_shapes/" + choice) document[picName].src=(url); } } The dropdown menu just has options written like this: Code: <li id="margin1"><a href="javascript:passit4('entire')" onMouseover="change1('pic8','image34')" onclick="ShowContent('uniquename3'); change3('pic2','entire');" onMouseout="change1('pic8','image_off');">sample link (entire)</a></li> Hello CodingForums. Im having problems making my hidden divs close and open how I want them to. I have one hidden div which opens and reveals and login area. but near the submit button I have a signup button and I want the login div to close and the sign up one to open but I can't get the login one to close. I'm sure you experts could fix this easily for me. Javascript: Code: <script language="javascript"> function toggle() { var ele = document.getElementById("loginbox"); 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"; } } function signup() { var ele = document.getElementById("signup"); var login = document.getElementById("loginbox"); var text = document.getElementById("displayText"); if(ele.style.display == "block") { ele.style.display = "none"; text.innerHTML = "show"; if(login.style.display == "block") { login.style.display = "none"; text.innerHTML = "hide"; } else { } } else { ele.style.display = "block"; text.innerHTML = "hide"; } } </script> Html: Code: <div id="signup" style="display: none"> <p> test </p> </div> <div id="loginbox" style="display: none"> <form id="loginscript" action="search.php" method="post"> <p>Your Username:<input type="text" name="Username" /></p> <p>Your Password:<input type="password" name="Passwrod" /></p> <input type="submit" class="hiddenbutton" value="login" /> <input type="reset" class="hiddenbutton" value="reset" /> <a href="javascript:signup();"><input type="button" class="hiddenbutton" value="signup" /></a> </form> </div> <a href="javascript:toggle();"><input type="submit" class="topbutton" name="login" value="Login In or Sign Up" /></a> I have a form and it allows someone to enter a user level, rather number 1 or 5, so I made this code to check if its 1 or 5, but it doesnt work, I get the alert everytime no matter what number I enter. I am new to javascript, but I just cant see anything wrong with this. PHP Code: var ul=document.forms["edit"]["userlevel"].value; if (ul != 1 || ul != 5) { alert("Please enter a user level 1 or 5"); return false; } Thanks Ben |