Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>Tried to run in JSFiddle and couldn't get it to work the way I want but eh might just be on my side or maybe I did something wrong</em></p> <p>What I would do: Make the button's have different ID's such as:</p> <p><code>&lt;input type = "button" id="weightCalculate" value = "calculate" onclick = "Calc(this)" /&gt;</code></p> <p>Something like so:</p> <pre><code> Select Conversion Type: &lt;br /&gt; &lt;input type = "radio" id="length" name = "rad1" onclick="showSecond(this)" /&gt;Length &lt;br /&gt; &lt;input type = "radio" id="weight" name = "rad1" onclick="showSecond(this)" /&gt;Weight &lt;br /&gt; &lt;input type = "radio" id="volume" name = "rad1" onclick="showSecond(this)" /&gt;Volume &lt;br /&gt; &lt;div id = "div" style="display:none"&gt; Select Direction: &lt;br /&gt; &lt;input type = "radio" name = "rad2" value="PG" checked="true" id="pTok"/&gt;Pounds to Kgs &lt;br /&gt; &lt;input type = "radio" name = "rad2" value="KP" id="kTop" /&gt;Kgs to Pounds &lt;br /&gt; Number to be converted: &lt;input type = "text" id="weightAmount" name = "amount" /&gt; &lt;br /&gt; &lt;input type = "button" id="weightCalculate" value = "calculate" onclick = "Calc(this)" /&gt; &lt;br /&gt; Result: &lt;br /&gt; &lt;input type ="text" id="weightFin" name ="fin" value="" readonly="readonly"/&gt; &lt;/div&gt; &lt;div id = "div1" style="display:none"&gt; Select Direction: &lt;br /&gt; &lt;input type = "radio" name = "rad3" value="FM" checked="true" id="fTom" /&gt;Feet to meters &lt;br /&gt; &lt;input type = "radio" name = "rad3" value="MF" id="mTof" /&gt;Meters to Feet &lt;br /&gt; Number to be converted: &lt;input type = "text" id="lengthAmount" name = "amount" value=""/&gt; &lt;br /&gt; &lt;input type = "button" id="lengthCalculate" value = "calculate" onclick = "Calc(this)" /&gt; &lt;br /&gt; Result: &lt;br /&gt; &lt;input type="text" id="lengthFin" value ="" readonly="readonly"/&gt; &lt;/div&gt; &lt;div id = "div2" style="display:none"&gt; Select Direction: &lt;br /&gt; &lt;input type = "radio" name = "rad4" value="GL" id="gTol" checked="true" /&gt;Gallons to Liters &lt;br /&gt; &lt;input type = "radio" name = "rad4" value="LG" id="lTog" /&gt;Liters to Gallons &lt;br /&gt; Number to be converted: &lt;input type = "text" id="volumeAmount" name = "amount" /&gt; &lt;br /&gt; &lt;input type = "button" id="volumeCalculate" value = "calculate" onclick = "Calc(this)"&gt; &lt;br /&gt; Result: &lt;br /&gt; &lt;input type ="text" id="volumeFin" value="" readonly="readonly"/&gt; &lt;/div&gt; </code></pre> <p>Then when you get this function call you find which is called and send it to be calculated depending on what button was clicked.</p> <pre><code> function showSecond() { if (document.getElementsByTagName("input")[0].checked) { div1.style.display = "block"; div.style.display = "none"; div2.style.display = "none"; } else if (document.getElementsByTagName("input")[1].checked) { div1.style.display = "none"; div.style.display = "block"; div2.style.display = "none"; } else { div1.style.display = "none"; div.style.display = "none"; div2.style.display = "block"; } } function Calc(obj) { if (obj.id == "weightCalculate") { var amt = document.getElementById("weightAmount").value; doWeightCalc(amt); } if (obj.id == "volumeCalculate") { var amt = document.getElementById("volumeAmount").value; doVolumeCalc(amt); } if (obj.id == "lengthCalculate") { var amt = document.getElementById("lengthAmount").value; doLengthCalc(amt); } } function doWeightCalc(amt) { var res = document.getElementById("weightFin") if (document.getElementById("pTok").checked) { var ch = amt * 2.2; res.value = ch; } else { var namt = amt / 2.2; res.value = namt } } function doVolumeCalc(amt) { var res = document.getElementById("volumeFin"); if (document.getElementById("gTol").checked) { var ch = amt * 3.78541178; res.value = ch; } else { var namt = amt * 0.264172052; res.value = namt } } function doLengthCalc(amt) { var res = document.getElementById("lengthFin"); if (document.getElementById("fTom").checked) { var ch = amt * 0.3048; res.value = ch; } else { var namt = amt * 3.2808399; res.value = namt } }​ </code></pre> <p>I would recommend you change the fact that you're using names to ID's. Such as lengthAmount, weightAmount, and volumeAmount...</p> <p>Also, I added an ID to the weight fin and used getElementById...</p> <p>EDIT: alright I did all the functions this works! o.O!</p> <p><a href="http://jsfiddle.net/CTRFU/6/" rel="nofollow">JSFiddle</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload