My javascript function evaluates differently in Chrome/Firefox vs Safari/Opera. There are a number of answers (i.e. here) about my overall goal of show/hide. I'd rather not use jQuery for this case. Anyway, the main problem I want to address is that the conditionals are not returning the same across all browsers.
If I run my code in Chrome or Firefox, I get the alert: "Foo - New Function Ran." If I run the same code in Safari or Opera, I get the alert: "Foo - Else Function Ran."
I don't know why this is browser specific. I'm happy to go read some additional threads or docs if this is addressed elsewhere, but I can't seem to figure it out.
// Edit: Per request, code simplified to show only relevant portions.
function bar(){
var addGiftVal = document.forms["subscribe"]["addGift"].value;
if (addGiftVal == "yes"){
alert("Bar - Yes Function Ran.");
}
else {
alert("Bar - Else Function Ran.");
}
}
function foo(){
var subTypeVal = document.forms["subscribe"]["sub_type"].value;
var subShipVal = document.forms["subscribe"]["ship_address_check"].value;
if (subTypeVal == "renewal"){
alert("Foo - Renewal Function Ran.");
}
else if (subTypeVal == "new"){
alert("Foo - New Function Ran.");
}
else {
alert("Foo - Else Function Ran.");
}
}
<body onLoad="foo();">
<form name="subscribe" id="subForm">
<p><input type="radio" name="sub_type" value="new" id="subf_new" checked="checked" onclick="foo();"> <label for="subf_new">New</label>
<input type="radio" name="sub_type" value="renewal" id="subf_renewal" onclick="foo();"> <label for="subf_renewal">Renewal</label>
<input type="radio" name="sub_type" value="gift" id="subf_gift" onclick="foo();"> <label for="subf_gift">Gift</label> </p>
<p><input type="radio" name="ship_address_check" id="subf_same" value="same" checked="checked" onclick="foo();"><label for="subf_same">Same</label> </p>
<p><input type="radio" name="ship_address_check" id="subf_different" value="different" onclick="foo();"><label for="subf_different">Different</label> </p>
<p>Yes or No?</p>
<input type="radio" name="addGift" id="subf_yes" value="yes" onclick="bar();"><label for="subf_yes">Yes</label>
<input type="radio" name="addGift" id="subf_no" value="no" onclick="bar();"><label for="subf_no">No</label>
<div id="submitBox"><input type="submit" value="Submit">
</form>
</body>