Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/203753-function-not-functioning/
Share on other sites

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>

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.