Wayniac Posted June 3, 2010 Share Posted June 3, 2010 I'm attempting to have the example code section which is currently commented out to keep from code spamming this forum to react with the IF statement created within the FUNCTION that I called "calc". The code in the example section does work, however the FUNCTION or IF statement is not working. Could anyone help me visualize the problem. Thank you <select name="ship_country" id="ship_country" style="min-width:200px;" onclick="calc;"> <option value="" selected="selected"></option> <option value="United States">United States</option> <option value="Canada">Canada</option> </select> <script type="text/javascript"> function calc() { if (document.getElementById("ship_country").value = ("United States") { // Example comment representing a lot nonsequential code pertaining to this topic. } } </script> Quote Link to comment Share on other sites More sharing options...
trq Posted June 3, 2010 Share Posted June 3, 2010 Select elements don't trigger an onClick events, I would start by changing that to onChange. Quote Link to comment Share on other sites More sharing options...
prestonwinfrey Posted June 3, 2010 Share Posted June 3, 2010 Use the onchange event instead of the onclick event to call your function. Attach parentheses to your function call which be empty if you are passing no parameters. You need to use "==" (is equal to) in your conditional statement to determine if what is on the left of the operator is equal to what is on the right. You don't need to enclose either side of the argument in parentheses or semi-colons. Make sense? Let me know if you need any further assistance. <script type="text/javascript"> function calc() { if (document.getElementById('ship_country').value == 'United States') { alert('Hello World!'); } } </script> <select name="ship_country" id="ship_country" style="min-width:200px;" onchange="calc()"> <option value="" selected></option> <option value="United States">United States</option> <option value="Canada">Canada</option> </select> Quote Link to comment Share on other sites More sharing options...
Wayniac Posted June 3, 2010 Author Share Posted June 3, 2010 Thank you everyone for replying. Preston, this worked very well and appreciate your assistance. Its still not quite working, but is almost there. Below is my full code (shortened down). The selection for united states works well. Although I may have my brackets in the wrong placement. There the last two at the bottom. Only reason I say this is because the contents inside the main IF statement are not working. Any ideas? <script type="text/javascript"> //var ship_country = document.getElementById("ship_country").value; //function calc() { //if (document.getElementById("ship_country").value = ("United States") { //function calc(sel) { //if ( sel.value == "United States" ) { function calc() { if (document.getElementById('ship_country').value == 'United States') { //alert('US Selection works'); function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places var v1 = document.getElementById("combo_1").value, v2 = document.getElementById("combo_0").value; var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength); var newnumber2 = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength); document.price.sub_total.value = newnumber.toFixed(2); // Output the result to the form field document.price.amount.value = (newnumber2 / v1).toFixed(2); } window.onload = function() { var dropChange = function() { // Declaring variables var v1 = document.getElementById("combo_1").value, v2 = document.getElementById("combo_0").value; var amount3 = document.getElementById("txt_price3").value, amount = document.getElementById("txt_price").value; document.getElementById("txt_price3").value = (amount); document.getElementById("quantity").value = (v1); if (v2 == 1) { document.getElementById("state").value = ("AL"); } else if (v2 == 2) { document.getElementById("state").value = ("AK"); } else if (v2 != (60 || 61 || 62 || 63 || 64 || 65 || 70)) { if (v1 == 1) { document.getElementById("txt_price2").value = v1 * 24.95 + 14.70; } else if (v1 == 2) { document.getElementById("txt_price2").value = v1 * 24.95 + 15.10; } } }; document.getElementById("combo_0").onchange = dropChange; document.getElementById("combo_1").onchange = dropChange; }; } } </script> Quote Link to comment Share on other sites More sharing options...
prestonwinfrey Posted June 3, 2010 Share Posted June 3, 2010 One quick thing I noticed is the way you're setting values. For example, you're doing this: document.getElementById("state").value = ("AL"); You should be doing this: document.getElementById("state").value = "AL"; Quote Link to comment Share on other sites More sharing options...
Wayniac Posted June 3, 2010 Author Share Posted June 3, 2010 Made that change, but still not working. (Thank you for the heads up though ) Quote Link to comment Share on other sites More sharing options...
prestonwinfrey Posted June 3, 2010 Share Posted June 3, 2010 You cannot define a function within a function. You need to separate the roundnumber() and calc() functions. Within calc(), you can call roundnumber() to return the results you are looking for. For further assistance, please post your HTML code. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.