JavaScript - Unit Converter With Two Dropdown Boxes
I am trying to make a unit converter with two dropdown boxes, two textfields and a button. I want to enter a number into the first textfield, select a unit option, then select another appropriate unit option based on the first selection, press the button and get the result in the second textfield. What would be the best approach for this. So far I have:
<html> <head> <script type="text/javascript"> function convertUnit () { // convert pound to kilogram document.unitForm.toField.value = document.unitForm.fromField.value / 2.2; // convert kilogram to pound document.unitForm.toField.value = document.unitForm.fromField.value * 2.2; // convert inch to centimeter document.unitForm.toField.value = document.unitForm.fromField.value / 2.54; // convert centimeter to inch document.unitForm.toField.value = document.unitForm.fromField.value * 2.54; } </script> </head> <body style="background-color:lightgray"> <form action="" name="unitForm"> <input type="text" name="fromField" onfocus="this.form.fromField.value=''; style.background='lightyellow';" onblur="style.background='white';" /> <select name="fromList" > <option value="" selected="selected">Unit</option> <option id="fromPound" value="2.2">lb</option> <option id="fromKilogram" value="1.0">kg</option> <option id="fromInch" value="1.0">in</option> <option id="fromCentimeter" value="2.54">cm</option> </select> <input type="button" value="Convert" onclick="convertUnit()" /> <input type="text" name="toField" readonly="readonly" /> <select name="toList"> <option value="" selected="selected">Unit</option> <option name="toKilogram" id="toKilogram" value="1.0">kg</option> <option name="toPound" id="toPound" value="2.2">lb</option> <option name="toCentimeter" id="toCentimeter" value="2.54">cm</option> <option name="toInch" id="toInch" value="1.0">in</option> </select> <input type="reset" value="Reset" /> </form> </body> </html> Similar TutorialsHi all -- I realize this is probably pretty simple, but it's been a while since I did any Javascripting, and things have changed a great deal. My apologies if I sound a bit clueless. I need to create a conversion tool. On one side of the tool are 4 form boxes. On the other, just text (not in form boxes). Under the covers, I have formulas for converting the numbers -- mostly basic math functions. So, the user types in numbers, clicks convert and the text changes. For some reason, I'm having a hard time getting this to go. How can I get the results of these calculations to show up as text within a DIV tag and not inside a form element? There are lots of examples of getting these calculations to appear within form boxes, but not within DIVs. Any examples would be greatly appreciated. This page is already using the Dojo toolkit for some other functionality, is this something I could leverage here to build my Javascript? Thanks in advance for your patience. Hi everybody. I'm new to both Javascript and this site, so apologies if this isn't the right way to be asking this question. Here's what I'm trying to do. I've got two dropdown boxes ('language' and 'word'). When the submit button is pressed, I want a div to show with the right word in it. I can do this fine with one dropdown box, but two has got me stumped. Any ideas would be much appreciated. S Hi I have the following currency converter code which works fine, but I need to make it possible for someone else to edit the euro price using a visual editor such as contribute. 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 language='javascript'> exchRate = 1.13; function getUKP(price) { document.write('(£' + Math.round(price / exchRate) + ')'); } </script> </head> <body> <strong>€526</strong> <script type="text/javascript">getUKP('526');</script><br /><br /> <strong>€1999</strong> <script type="text/javascript">getUKP('1999');</script> </body> </html> This code requires someone to change the figure in the javascript as well as the html. I guess I need to somehow automatically pick up the numeric characters only from the html which appear on the same line of code... or perhaps there's an easier way.? Any help would be much appreciated. Thanks Hello, I've been reading my JS book, have done a 'Degrees Converter' which converts fahrenheit into centigrade. There is a prompt box which automatically loads when the website is loaded. I am pressuming this is because I have my script in the body. I have a question if anybody could possibly help me out. How do I get my script as a link? To explain a little better, I wanted to put the words 'Degrees Converter' onto my page(as a link), so that when people click the link, it loads up the prompt box. I'm very new to JavaScript, so if anyone could help me out it would be a great help. Thank you very much. Hi all, I have a problem about my converter.Why when I'm clicking on the keypad displayed in the form,it doesn't display the result.But when clicking on the keypad on the keyboard,it works?.Plz help me with this trouble.Here are my code. <-html-> <html> <head> <title>Currency Converter</title> <link rel="stylesheet" href="../css/cal.css" type="text/css" > <script type="text/javascript" src="../cal.js"></script> </head> <body> <h2>Currency Converter<h2> <table id="table1"> <form id="currconverter"> <tr> <td>Convert<br>from:</td> <td><input type="text" id="amount" value="" size="17" onChange="convertCurr()"></td> <td> <select id="from_select"> <option value="GBP" selected="selected">Pound Sterling</option> <option value="USD">US Dollar</option> <option value="MUR">Mauritian RS</option> <option value="EUR">Euro</option> <option value="JPY">Yen</option> </select> </td> </tr> <tr> <td>Convert<br>to:</td> <td><input type="text" id="result" value="" size="17" readonly="readonly" onchange="convertCurr();"></td> <td> <select id="to_select"> <option value="GBP">Pound Sterling</option> <option value="USD">US Dollar</option> <option value="MUR" selected="selected">Mauritian RS</option> <option value="EUR">Euro</option> <option value="JPY">Yen</option> </select> </td> </tr> </form> </table> <br> <table cellpadding="0" cellspacing="5" border="1" align="center" bgcolor="AABBCC"> <form name="cal"> <tr> <td><input type="button" value="7" onClick="currconverter.amount.value += '7'"></td> <td><input type="button" value="8" onClick="currconverter.amount.value += '8'"></td> <td><input type="button" value="9" onClick="currconverter.amount.value += '9'"></td> </tr> <tr> <td><input type="button" value="4" onClick="currconverter.amount.value += '4'"></td> <td><input type="button" value="5" onClick="currconverter.amount.value += '5'"></td> <td><input type="button" value="6" onClick="currconverter.amount.value += '6'"></td> </tr> <tr> <td><input type="button" value="1" onClick="currconverter.amount.value += '1'"></td> <td><input type="button" value="2" onClick="currconverter.amount.value += '2'"></td> <td><input type="button" value="3" onClick="currconverter.amount.value += '3'"></td> </tr> <tr> <td><input type="button" value="0" onClick="currconverter.amount.value += '0'"></td> <td><input type="button" value="." onClick="currconverter.amount.value += '.'"></td> <td><input type="reset" value="C" OnClick="currconverter.amount.value = ''"></td> </tr> </form> </table> </body> </html> <-javascript-> function convertCurr() { var amount = parseFloat(document.getElementById("amount").value); var form = document.getElementById("currConverter"); var fromSelect = document.getElementById("from_select"); var toSelect = document.getElementById("to_select"); var result= document.getElementById("result"); var currencyTo = []; switch (fromSelect.value) { case "GBP": currencyTo = [1, 1.61, 46.91, 1.23, 128.89]; break; case "USD": currencyTo = [1.62, 1, 28.90, 0.76, 79.84]; break; case "MUR": currencyTo = [1.02, 0.03, 1, 0.03, 0.00]; break; case "EUR": currencyTo = [0.81, 1.31, 38.01, 1, 104.47]; break; case "JPY": currencyTo = [0.01, 1.25, 0.00, 0.96, 1]; break; default: alert("It broke."); } result.value = (amount * currencyTo[toSelect.selectedIndex]).toFixed(2); } Hello, I am having trouble with a javascript length converter that I got from http://www.javascriptkit.com/script/...onverter.shtml It is working fine in dreamweaver, but when it goes live on my site, the calculations do nothing. I can see everything in the drop down menus, but when i click the "to" button, nothing happens. I got the following errors from chrome when activating any of the calculation functions. I'm wondering if anyone here can help me decipher these 1. Uncaught TypeError: Cannot read property 'from_unit' of undefined 1. convert_unitDefault.aspx:459 2. (anonymous function)Default.aspx:482 3. onchangeDefault.aspx:483 1. Default.aspx:459Uncaught TypeError: Cannot read property 'from_unit' of undefined 1. convert_unitDefault.aspx:459 2. (anonymous function)Default.aspx:496 3. onchangeDefault.aspx:497 1. Default.aspx:459Uncaught TypeError: Cannot read property 'from_unit' of undefined 1. convert_unitDefault.aspx:459 2. (anonymous function)Default.aspx:516 3. onclickDefault.aspx:517 Hey guys. I am in an introductory computer science class and I am having trouble writing JavaScript code for a currency converter. I can get it to where it spits out numbers, but its always the same numbers. I think there's something wrong in the calculations part of my code but I can't figure it out. I am posting it below, and any help would be greatly appreciated. Thank you. <html> <head> <title>CURRENCY CONVERTER</title> <script language="JavaScript"> function converting() { with (document.conversions) { if (unit[1].selected) { var result = eval(unit.value*0.974); convertedValue.value = result; } // end if if (unit[2].selected) { var result = eval(unit[2].value*0.492); convertedValue.value = result; } // end if if (unit[3].selected) { var result = eval(unit[3].value*0.706); convertedValue.value = result; } // end if } // end with } // end function converting </script> </head> <form name = "conversions"> <table border=6 bgcolor ="yellow"> <tr> <td><font color="red" size=6>Enter amount in U.S. $:</font></td> <td><input type="text" name="inUnit" size = 4></td> </tr> <tr> <td><font color="red" size=6>Select target unit:</font> <td><select name="unit"> <option value="Select Currency" selected>Choose one... <option value="1">Canadian Dollars <option value="2">British Pounds <option value="3">Euros </select> <td><input type="button" value="Convert" onclick="converting()"> </tr> <tr> <td><font color="green" size=6>Converted Value:</font> <td><input type="text" name="convertedValue" size = 4> <td><input type="reset" value="Reset"> </tr> </table> </form> </body> </html> Hello all. I'm so glad i found this forum!!! This looks like a great coding community. This will be my First Post. I was wondering if you guys can help me with my weight converter. I cant seem to make it work properly on my site. here is the code im using: <p class="about">Convert weight measurements between U.S./Imperial and SI (Metric) units<br /> Type your value in a box, click the <input onclick="alert('Not this button!')" type="button" name="test_bt" value="Go!" /> and all unit conversions in the same category will be calculated.</p> <form action=""> <table width="500" border="1" cellspacing="0" cellpadding="4"> <tbody> <tr bgcolor="#0066ff"><th class="headings" colspan="3" bgcolor="#1765BB">SI (Metric)</th></tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78" width="288">Tonne</td> <td class="headings" align="right" bgcolor="#103e78" width="130"><input type="text" name="mTon" size="15" /></td> <td class="headings" bgcolor="#103e78" width="57"><input onclick="if (checkNum(mTon.value)) compute(this.form,mTon.name,weight_data)" type="button" name="mTon_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78" width="288">Kilogram (kg)</td> <td class="headings" align="right" bgcolor="#103e78" width="130"><input type="text" name="mKilogram" size="15" /></td> <td class="headings" bgcolor="#103e78" width="57"><input onclick="if (checkNum(mKilogram.value)) compute(this.form,mKilogram.name,weight_data)" type="button" name="mKilogram_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78" width="288">Gram (g)</td> <td class="headings" align="right" bgcolor="#103e78" width="130"><input type="text" name="mGram" size="15" /></td> <td class="headings" bgcolor="#103e78" width="57"><input onclick="if (checkNum(mGram.value)) compute(this.form,mGram.name,weight_data)" type="button" name="mGram_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78" width="288">Milligram (mg)</td> <td class="headings" align="right" bgcolor="#103e78" width="130"><input type="text" name="mMilligram" size="15" /></td> <td class="headings" bgcolor="#103e78" width="57"><input onclick="if (checkNum(mMilligram.value)) compute(this.form,mMilligram.name,weight_data)" type="button" name="mMilligram_bt" value="Go!" /></td> </tr> <tr><th class="headings" colspan="3" bgcolor="#103e78"> </th></tr> <tr bgcolor="#0066ff"><th class="headings" colspan="3" bgcolor="#1765BB">Troy weight</th></tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78" width="288">Pound (lb t)</td> <td class="headings" align="right" bgcolor="#103e78" width="130"><input type="text" name="troyPound" size="15" /></td> <td class="headings" bgcolor="#103e78" width="57"><input onclick="if (checkNum(troyPound.value)) compute(this.form,troyPound.name,weight_data)" type="button" name="troyPound_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78" width="288">Ounce (oz t)</td> <td class="headings" align="right" bgcolor="#103e78" width="130"><input type="text" name="troyOunce" size="15" /></td> <td class="headings" bgcolor="#103e78" width="57"><input onclick="if (checkNum(troyOunce.value)) compute(this.form,troyOunce.name,weight_data)" type="button" name="troyOunce_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78" width="288">PennyWeight (dwt)</td> <td class="headings" align="right" bgcolor="#103e78" width="130"><input type="text" name="troyDWT" size="15" /></td> <td class="headings" bgcolor="#103e78" width="57"><input onclick="if (checkNum(troyDWT.value)) compute(this.form,troyDWT.name,weight_data)" type="button" name="troyDWT_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78" width="288">Grain<br /> Same as Avoirdupois grain</td> <td class="headings" align="right" bgcolor="#103e78" width="130"><input type="text" name="troyGrain" size="15" /></td> <td class="headings" bgcolor="#103e78" width="57"><input onclick="if (checkNum(troyGrain.value)) compute(this.form,troyGrain.name,weight_data)" type="button" name="troyGrain_bt" value="Go!" /></td> </tr> <tr bgcolor="#6699ff"> <td class="headings" colspan="3" bgcolor="#103e78"> </td> </tr> <tr bgcolor="#0066ff"><th class="headings" colspan="3" bgcolor="#1765BB">Avoirdupois weight</th></tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78">British ("long") ton</td> <td class="headings" align="right" bgcolor="#103e78"><input type="text" name="briTon" size="15" /></td> <td class="headings" bgcolor="#103e78"><input onclick="if (checkNum(briTon.value)) compute(this.form,briTon.name,weight_data)" type="button" name="briTon_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78">US ("short") ton</td> <td class="headings" align="right" bgcolor="#103e78"><input type="text" name="usTon" size="15" /></td> <td class="headings" bgcolor="#103e78"><input onclick="if (checkNum(usTon.value)) compute(this.form,usTon.name,weight_data)" type="button" name="usTon_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78" height="42">British ("long") hundredweight (cwt)</td> <td class="headings" align="right" bgcolor="#103e78" height="42"><input type="text" name="briCWT" size="15" /></td> <td class="headings" bgcolor="#103e78" height="42"><input onclick="if (checkNum(briCWT.value)) compute(this.form,briCWT.name,weight_data)" type="button" name="briCWT_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78">US ("short") hundredweight (cwt)</td> <td class="headings" align="right" bgcolor="#103e78"><input type="text" name="usCWT" size="15" /></td> <td class="headings" bgcolor="#103e78"><input onclick="if (checkNum(usCWT.value)) compute(this.form,usCWT.name,weight_data)" type="button" name="usCWT_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78">Stone</td> <td class="headings" align="right" bgcolor="#103e78"><input type="text" name="briStone" size="15" /></td> <td class="headings" bgcolor="#103e78"><input onclick="if (checkNum(briStone.value)) compute(this.form,briStone.name,weight_data)" type="button" name="briStone_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78">Pound (lb)</td> <td class="headings" align="right" bgcolor="#103e78"><input type="text" name="avdpPound" size="15" /></td> <td class="headings" bgcolor="#103e78"><input onclick="if (checkNum(avdpPound.value)) compute(this.form,avdpPound.name,weight_data)" type="button" name="avdpPound_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78">Ounce (oz)</td> <td class="headings" align="right" bgcolor="#103e78"><input type="text" name="avdpOunce" size="15" /></td> <td class="headings" bgcolor="#103e78"><input onclick="if (checkNum(avdpOunce.value)) compute(this.form,avdpOunce.name,weight_data)" type="button" name="avdpOunce_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78">Dram (dr)</td> <td class="headings" align="right" bgcolor="#103e78"><input type="text" name="avdpDram" size="15" /></td> <td class="headings" bgcolor="#103e78"><input onclick="if (checkNum(avdpDram.value)) compute(this.form,avdpDram.name,weight_data)" type="button" name="avdpDram_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" bgcolor="#103e78">Grain</td> <td class="headings" align="right" bgcolor="#103e78"><input type="text" name="avdpGrain" size="15" /></td> <td class="headings" bgcolor="#103e78"><input onclick="if (checkNum(avdpGrain.value)) compute(this.form,avdpGrain.name,weight_data)" type="button" name="avdpGrain_bt" value="Go!" /></td> </tr> <tr bgcolor="#0066ff"> <td class="headings" colspan="2" align="center" bgcolor="#103e78"><input onclick="resetAll(this.form)" type="button" name="res7" value="Clear all numbers" /></td> <td class="headings" bgcolor="#103e78"> </td> </tr> </tbody> </table> <p> </p> </form> <p> </p> and help will be greatly appreciated. Hi, I need to create a currency converter using a drop down menu. Firstly i needed to do a regular expressions check to make sure only numerics are entered and I got that. The one thing im pretty confused on is how to do the calculations in the actual function. Ive popped the code below, and if anyone could give me some hints on how to get the data from the drop down menu and calculate it properly that would be awesome. Thanks. -oh and i know all my coding is here but i think its relevent that you can view it all. Code: <html> <head> <style type ="text/css"> @import url(style.css); </style> <script type="text/javascript"> function convert() { var OK = true; var inDollar= document.getElementById('dollars').value; var regex= /^\d{1,}$/; if (!regex.test(inDollar)) { OK=false; alert('Australian Dollars must be entered as numeric with no decimal places'); } return OK; var rate= country.conversion.value; if (document.getElementByID('conversion').selectedIndex == 0) { var rate= country.conversion.value; var convert = inDollar*rate; alert('$'+inDollar+' converts to'+rate+' US Dollars'); } if (document.getElementByID('conversion').selectedIndex == 1) { var rate= country.conversion.value; var convert = inDollar*rate; alert('$'+inDollar+' converts to'+rate+' Euro'); } if (document.getElementByID('conversion').selectedIndex == 2) { var rate= country.conversion.value; var convert = inDollar*rate; alert('$'+inDollar+' converts to'+rate+' Yuan'); } if (document.getElementByID('conversion').selectedIndex == 3) { var rate= country.conversion.value; var convert = inDollar*rate; alert('$'+inDollar+' converts to'+rate+' Yen'); } } </script> </head> <body> <h1>Currency Page</h1> <form> Australian Dollars:<input type="text" name="dollars" id="dollars" /> <br /> Convert to: <select id='conversion' name='country'> <option value=".73">US Dollars</option> <option value=".62">Euro</option> <option value="5.8">Chinese Yuan</option> <option value="78.5">Japanese Yen</option> </select> <br /> <input type="button" value="Convert" onClick="convert()" /> </form> </body> </html> I can only seem to find really complicated ones online, nothing simple which i think this is. Thanks I'm currently attempting to make a unit conversion tool. I'd like for the user to select which unit to start and which unit to convert too. EX: 1 foot comes out to 12 inches. So far it's not going well. I have two list boxes, named "firstmeasure" and "secondmeasure". For the purpose of this post we will use only 3 different unit in the list boxes, centimeters, feet, and inches. I also have two text boxes used to show the output and for the user to put in a number to convert. And of course, a button that calls the function (I'd prefer it to do the function automatically, but I'll get to that later). I'm trying to use variable arrays and using the value of the list box options to define which value the array will use. Then in the function I'm trying to use a switch/case statement to allow it to figure out which numbers to plug into an equation. My problem is that it seems to automatically go to the last case option regardless of what the list box is. This is the form that in the HTML file: Code: <form action="" name="lengthform" id="lengthform" > <p> <label >Select first measurement</label> <br/> <select id="firstmeasure" name="firstmeasure" > <option value="1">Centimeter</option> <option value="2">Feet</option> <option value="3">Inch</option> </select> <input type="text" id="input" name="input" value="1" /> <br/> <br/> <label >Select Second measurement</label> <br/> <select id="secondmeasure" name="secondmeasure" > <option value="1">Centimeter</option> <option value="2">Feet</option> <option value="3">Inch</option> </select> <input type="text" id="output" name="output" /> </p> <input type="button" onclick="calculate()" value="Convert!" /> </form> This is whats in the Java file: Code: var centimeters1 = new Array() ; centimeters1["1"] = "1" ; //centimeters centimeters1["2"] = "0.032808399" ; //feet centimeters1["3"] = "0.393700787" ; //inches var feet1 = new Array() ; feet1["1"] = "30.48" ; //centimeters feet1["2"] = "2" ; //feet feet1["3"] = "12" ; //inches var inches1 = new Array() ; inches1["1"] = "2.54" ; //centimeters inches1["2"] = "0.0833333333" ; //feet inches1["3"] = "3" ; //inches function calculate() { var measure1 = document.lengthform.elements["firstmeasure"] ; var measure2 = document.lengthform.elements["secondmeasure"] ; var A = document.lengthform.input.value ; var B ; var C ; switch (measure1.value){ case "1" : B = centimeters1[measure2.value] ; C = A * B document.lengthform.output.value = C case "2" : B = feet1[measure2.value] ; C = A * B document.lengthform.output.value = C case "3" : B = inches1[measure2.value] ; C = A * B document.lengthform.output.value = C }//end switch }//end function So, measure1 reads the value of the first list box, and measure 2 reads the value of the second list box. I tried using a switch case statement instead of writing a ton of if statements. Which I also tried and it gave me the same problem. If there is a much better way to do a conversion tool, than I would love to learn how. Any help on fixing this seemingly easy problem would be great. Edit: Do I have to use an "int" variable in the switch statement? I'm using Adobe Dreamweaver and it won't allow to me to create a variable using "int", it just gives me a syntax error. Hi, I'm trying to integrate an address finder (http://www.craftyclicks.co.uk/) into my shopping cart (OsCommerce). I can get it to work but I need to add my own functionality. I'm not very experienced with JavaScript and my head has entered an infinite loop by now. The problem is that the address finder script can change the selected country in a drop-down list depending on the postcode entered by the user (using the onblur event handler). What I need it to do is to remove all other countries depending on the postcode. I can get it to remove all other countries but how do i return to the original list of countries when the postcode is changed again? Once all other counties are removed, the drop-down list will obviously only have one option left... I guess the question is also how does a function remember what it has done before, when it is called again? I have written this short test script as it is easier to work with than the craftyclicks oscommerce contribution: 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> <title>HTML Template</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="description" content="" /> <meta name="keywords" content="" /> <script type="text/javascript"> //<![CDATA[ function store(element) { // store values var cl = element; var text_list = new Array(); var value_list = new Array(); var length = cl.length; for (var foo=0; foo<length; foo++) { text_list[foo] = cl.options[foo].text; value_list[foo] = cl.options[foo].value; alert("text array " + foo + " " + text_list[foo]); alert("value array " + foo + " " + value_list[foo]); } populate(cl, text_list, value_list); } function populate(element, text, value) { // populate options with previously stored values var cl = element; var length = cl.length; cl.options.length=0; for (var bar=0; bar<length; bar++) { cl.options[bar]= new Option(text[bar], value[bar], false, false); } } function crafty_set_country(code) { var cl = document.getElementById('select'); store(cl); for (var i=0; i<cl.length; i++) { if (cl.options[i].value == code) { alert(cl.options[i].value + " found"); var value = cl.options[i].value; var text = cl.options[i].text; cl.options.length=0; cl.options[0]=new Option(text, value, true, true); /* for (var j=0; j<cl.length; j++) { alert("second loop " + cl.options[j].text); if (cl.options[i].value != code) { cl.options[j] } } */ } else { alert(cl.options[i].value); } } } //]]> </script> </head> <body> <form> <select id="select"> <option value="10">ten</option> <option value="20">twenty</option> <option value="30">thirty</option> <option value="40">fourty</option> <option value="50">fifty</option> <option value="60">sixty</option> </select> <input type="button" value="remove" name="button" onClick="crafty_set_country(50)"> <input type="button" value="repopulate" name="button" onClick="crafty_set_country(100)"> </form> </body> </html> Many thanks! Martin hey guys The first dropdown has some options as : abc(a) bcd(a) cde(b) def(b) efg(c) fgh(c) The second dropdown has : a b c On selecting abc(a) in first dropdown the 'a' must get selected in second dropdown,on selecting cde(b) second dropdown must have 'b' and so on,also the second dropdown value should be disabled(grayed out) for user.Need the code in javascript.ty in advance. I imagine this would be very simple for someone who knows javascript. I want to have three fields. First field is "posted speed limit", second field is "actual speed" and third field will be the output field. All the script needs to do it subtract the posted speed from the actual speed and add a ZERO to the end; which is the amount of the speeding ticket. The minimum fine is $100, however. So, 5 miles over the speed limit would be $100 (minimum value) 15 miles over the speed limit would be $150 (add a zero) 35 miles over the speed limit would be $350. etc. I know very little Javascript, if anyone could help me out with this, I'd appreciate it. Thanks, Sean Hello, I am completely new to HTML/JS/CSS, therefore I know very little. I have two drop-down prompt controls with month names. One has just one value (say "July") and the other has all the months of the year ("January".."December"). The first prompt control is hidden on the page. How do I set the default selection of the second prompt control to the value present in the first prompt control? So, when the page is run, the second prompt control should automatically show "July". I was reading up on the selectedIndex property (?), but I know that it won't work because I want Index 0 to be selected in the first control and Index 6 in the second, and I expect it to change every month (next month it will be index 7 that should be automatically selected). If it matters, I am using IE8. Thanks! Hi, I have two dropdown lists with the second one being dependent on the selection in the first. Options in list 1: 1,3 or 4 List two should be enabled when 3 or 4 is selected in list 1. So far so good, managed to get it to work with only one set of lists, but I actually have 18 of those sets in this form: Code: <select name="fw[$i]" id="fw[$i]"> <option value="1">FWH</option> <option value="3">links</option> <option value="4">rechts</option> <option value="0" selected></option> </select><br /> <select name="lie[$i]" id="lie[$i]" disabled="disabled" onchange="showBox($i,xxx);"> <option value="0" selected>---</option> <option value="1">gut</option> <option value="2">schlecht</option> <option value="3">Strafschlag</option> <option value="4">OB</option> </select> $i is my index generated by my PHP script and runs from 1 to 18. Everything works just fine as long as the index is not in play. With the index my function breaks, most probably due to my inability to extract the second parameter (xxx above) from the selection field. Here's my function, there could be something wrong there too with how exactly to specify the proper dropdown list to enable. Code: function showBox(field,val) { if (val > 1) { var box = 'lie[' + field + ']'; document.form1.box.disabled == false; } else { document.form1.box.disabled == true; } } Finally, I'm not that adept in javascript programming, more like a trial and error guy, how has hit the wall with this problem. Thanks in advance! Joe Hi Can anyone help with this.... I'm trying to create and object that has text on either side of the flip boxes and when the correct answers are showing (some will need to be flipped and some not). The boxes are disapled and a message appears to say this is correct. I need to work out how to write different text on the other side of each flip box How to differentiate between when the box is flipped and when it isnt (to assign a value) How to disable boxes when all are correct or how to add a subit button so that when users think the boxes are correct they click to check answers So far I have this and now im stuck, its not doing exactly what i want. I'm new to all this and just need a bit of guidance... Code: <style> .content { width: 880px; height:180px; } .test { width:800 height:100 } .answers{ position:absolute; width:25px; height:20px; left: 200px; top: 4px; } .correct{ background-repeat:no-repeat; background-image:url(assets/correct-small.png); z-index:1; } .incorrect{ background-repeat:no-repeat; background-image:url(assets/incorrect-small.png); z-index:1; } .flip { position:relative; float:left; margin-left:3px; margin-right:3px; width: 500px; height: 22px; padding: 3px; margin-bottom: 2px; border: 1px #CCC solid; background-color: #EFEFEF; text-align: center; } .flip:hover{ cursor: pointer; background-color: #FFF; } </style> <script language="javascript"> trueCount = 0; falseCount = 0; for(i=1;i<=15;i++) { $("#flipbox" + i).click(function(){ $(this).flip({ direction:'tb', color: '#EFEFEF', }); }); } function addAnswer(div, answer){ if($("#"+div).hasClass("correct")) { trueCount--; $("#"+div).removeClass("correct"); } else { $("#"+div).addClass("correct"); answersArray[trueCount] = $("#"+div).parent().html().substr(0, $("#"+div).parent().html().indexOf('<')); trueCount++; if(trueCount == 2) { $(".test").fadeOut(1000, function(){ $(this).html("<h3>Correct - </h3>" ).fadeIn(1000); }) } } } updateBookmark(); </script> <br /><br /> <div class="content"> <div class="flip" onclick="addAnswer('answer1','c')" id="flipbox1">Reading a book<div class="answers" id="answer1"></div></div> <div class="flip" onclick="addAnswer('answer2','t')" id="flipbox2">Position their body so that they are facing you <div class="answers" id="answer2"></div></div> <div class="flip" onclick="addAnswer('answer3','c')" id="flipbox3">Talk<div class="answers" id="answer3"></div></div> <div class="flip" onclick="addAnswer('answer4','t')" id="flipbox4">Make eye contact<div class="answers" id="answer4"></div></div> <div class="flip" onclick="addAnswer('answer5','c')" id="flipbox5">Ask questions<div class="answers" id="answer5"></div></div> </div> <div class="test"></div> Hello, I have a drop down box, which display an images. It is also saved through a cookie on what option you leave it on. Is there a way you can make it so there are several drop downs on the same page that will also display images corresponding to the option chosen? Here's a live example: www.cirula.com/options here is my coding Code: <script language="javascript"> function linkrotate(which){ var mylinks=new Array() //add in more links if you want (ie:mylinks[3]=...) mylinks[0]="http://www.google.com" mylinks[1]="http://www.ebay.com" mylinks[2]="http://www.yahoo.com" window.location=mylinks[which] } function showimage() { if (!document.images) return document.images.pictures.src= document.mygallery.picture.options[document.mygallery.picture.selectedIndex].value } </script> </head> <body onLoad="setDefaultValues()"> <div align="center"> <form name="mygallery"> <p><select name="picture" onChange="showimage(); setCookie(this.name,this.selectedIndex)" size="1"> <option value="http://www.cirula.com/images/google.png">Google</option> <option value="http://www.cirula.com/images/ebay.png">Ebay</option> <option value="http://www.cirula.com/images/yahoo.png">Yahoo</option> </select></p> </form> <p align="center"><a href="javascript:linkrotate(document.mygallery.picture.selectedIndex)" onMouseover="window.status='';return true"><img src="http://www.cirula.com/images/google.png" name="pictures" width="150" height="150" border=0></a> |