var s = document.getElementById('field[12]');
var cust_cc_exp_month = s.options[s.selectedIndex].value;

// This displays the correct Value -- which is "0" for item #0 = "Please select..."
alert("Month: " + cust_cc_exp_month.toString());
if (cust_cc_exp_month == "0") {
  // This code never executes!
}

Here is the HTML portion:

<select id="field[12]" name="field[12]">
<option value="0">MONTH</option>
<option value="1" <?php if ($field[12] == "1") echo "SELECTED" ?>>January</option>
<option value="2" <?php if ($field[12] == "2") echo "SELECTED" ?>>February</option>
<option value="3" <?php if ($field[12] == "3") echo "SELECTED" ?>>March</option>
</select>

I've tried using cust_cc_exp_month.toString(), and I've tried == 0 instead of == "0".
Nothing seems to work. To me, it defies all logic! There has to be something I'm overlooking.

Thanks for any help you can provide --

Matthew

Recommended Answers

All 2 Replies

Are you putting string values into field[12] ?

I just tried this test:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>Untitled Document</title>
	</head>
	<body>
<script>
window.onload = function() {
	var s = document.getElementById('field[12]');
	var cust_cc_exp_month = s.options[s.selectedIndex].value;
	
	// This displays the correct Value -- which is "0" for item #0 = "Please select..."
	alert("Month: " + cust_cc_exp_month.toString());
	if (cust_cc_exp_month == "0") {
	  // This code never executes!
	  alert('ok');
	}
};

</script>

<select id="field[12]" name="field[12]">
<option value="0">MONTH</option>
<option value="1" <?php if ($field[12] == "1") echo "SELECTED" ?>>January</option>
<option value="2" <?php if ($field[12] == "2") echo "SELECTED" ?>>February</option>
<option value="3" <?php if ($field[12] == "3") echo "SELECTED" ?>>March</option>
</select>
	</body>
</html>

After the window loads, alert('ok'); is executed as expected. I tested in FF2.0 and IE6,7.

I don't see how you could be having the problem you're having unless you have a JavaScript error somewhere thats preventing full execution of the JS.

a conditional such as:

if (cust_cc_exp_month == "0")

is equivalent to:

if (cust_cc_exp_month == false)

or

if (!cust_cc_exp_month)

since type's are not being evaluated.

If you had

if (cust_cc_exp_month === "0")

then the type would matter and an Integer 0 would be differentiated from the string '0' or the boolean false.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.