Reputation: 73
How should I write this or statement out in javascript?
I am trying to edit a script that redirects users to another page if they choose anything other than USA as a country. I would like to pool Canada with USA. So I want to redirect users that select anything other than USA or Canada. I tried to edit the script in various ways, but keep breaking it anytime I do. BTW I'm a total JS noob that is actively learning lil by lil.
Below is the snippit of the entire code I have edited
$(document).ready(function(){
var una = "ok";
$("#shipping-country").change(function(event){
if(($("#shipping-country").val() != "US") || $("#shipping-country").val() != "CA")&&(una=="ok")){
una="no";
$($("h4.ys_subSectionHeader")[4]).prepend('<p>Because you have selected an international address, you are being routed to Bongo International, our third party international payment and shipping partner."</p></br></br>');
$("#ys_paymentMethod").fadeOut(1);
$($("h4.ys_subSectionHeader")[1]).fadeOut(1);
$($("h4.ys_subSectionHeader")[2]).fadeOut(1);
$("#ys_giftWrap").fadeOut(1);
$("#ys_shippingOptions").fadeOut(1);
$($("div.ys_pageActions")[1]).fadeOut(1);
$('#ys_comments').append('<div id="newbox" style="position:relative; width:450px; height:150px ; top:20px; left: 0px">Because you have selected an <b>international address</b>, you are now being routed to <span style="font-weight:bold;color:#FF8C00">Bongo </span><span style="font-weight:bold;color:#32CD32">International</span>, our third party international payment and shipping partner. Shipping, duties, and taxes will be calculated on the next page.<br /><span id="clockBongoInt" style="font-weight:bold;color:red;" ></span> </div>');
$('#newbox').append('</br><input id="valor" class="ys_primary" type="button" value="Send Order" >');
$("#valor").click(function(event){
BongoCheckout.init();
$("#BongoCheckoutForm").submit(); });
Here is the original line
if(($("#shipping-country").val() != "US") &&(una=="ok")){
Here is how I have edited it, but it breaks the entire script
if(($("#shipping-country").val() != "US") || $("#shipping-country").val() != "CA")&&(una=="ok")){
Here is the script in it's entirety (using my edit)
var BongoCheckout = {
insertForm: function() {
var partnerKey="5e42cee1ae064ddb6dbed453d99d3d2"; // replace for your partney key from BongoUs
var per_item_shipping = false; // If this is set to 'true' then shipping value set below will apply to each ITEM
var shipping_cost = 15; // add your domestic shipping cost
var shipping_cost_breakdown = shipping_cost / numOfItems;
var firstname = $('#shipping-first-name').val();
var lastname = $('#shipping-last-name').val();
var companyname = $('#shipping-company').val();
var addres1 = $('#shipping-address1').val();
var addres2 = $('#shipping-address2').val();
var city = $('#shipping-city').val();
var postcode = $('#shipping-zip').val();
var phone = $('#shipping-phone').val();
var country= $("#shipping-country").val();
var email= $("#billing-email").val();
var state= $("#shipping-state").val();
$("#BongoCheckoutForm").append('<input type="hidden" name="PARTNER_KEY" value="'+partnerKey+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="CUST_FIRST_NAME" value="'+firstname+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="CUST_LAST_NAME" value="'+lastname+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="CUST_COMPANY" value="'+companyname+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="CUST_STATE" value="'+state+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="CUST_EMAIL" value="'+email+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="CUST_PHONE" value="'+phone+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="CUST_ZIP" value="'+postcode+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="CUST_CITY" value="'+city+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="CUST_ADDRESS_LINE_1" value="'+addres1+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="CUST_ADDRESS_LINE_2" value="'+addres2+'">');
//CUST_COUNTRY
$("#BongoCheckoutForm").append('<input type="hidden" name="CUST_COUNTRY" value="'+country+'">');
var custom =new Array;
for(i=0;i< qtys.length ;i++){
custom[i]="";
for(j=0;j< $($("td.ys_options")[i]).children().children().length; j++){
custom[i]= custom[i]+" "+$($($("td.ys_options")[i]).children().children()[j]).text();
}
$("#BongoCheckoutForm").append('<input type="hidden" name="PRODUCT_ID_'+(i+1)+'" value="'+codes[i]+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="PRODUCT_NAME_'+(i+1)+'" value="'+items[i]+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="PRODUCT_PRICE_'+(i+1)+'" value="'+price[i]+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="PRODUCT_Q_'+(i+1)+'" value="'+qtys[i]+'">');
$("#BongoCheckoutForm").append('<input type="hidden" name="PRODUCT_CUSTOM_1_'+(i+1)+'" value="'+custom[i]+'" /> ');
if (per_item_shipping) {
$('form[name="BongoCheckoutForm"]').append('<input type="hidden" name="PRODUCT_SHIPPING_'+(i+1)+'" value="'+shipping_cost.toString()+'"> ');
} else {
$('form[name="BongoCheckoutForm"]').append('<input type="hidden" name="PRODUCT_SHIPPING_'+(i+1)+'" value="'+shipping_cost_breakdown+'"> ');
}
}},
// Append and control the international shipping notification
init: function() {
this.insertForm();
}
}
$(document).ready(function(){
var una = "ok";
$("#shipping-country").change(function(event){
if(($("#shipping-country").val() != "US") || $("#shipping-country").val() != "CA")&&(una=="ok")){
una="no";
$($("h4.ys_subSectionHeader")[4]).prepend('<p>Because you have selected an international address, you are being routed to Bongo International, our third party international payment and shipping partner."</p></br></br>');
$("#ys_paymentMethod").fadeOut(1);
$($("h4.ys_subSectionHeader")[1]).fadeOut(1);
$($("h4.ys_subSectionHeader")[2]).fadeOut(1);
$("#ys_giftWrap").fadeOut(1);
$("#ys_shippingOptions").fadeOut(1);
$($("div.ys_pageActions")[1]).fadeOut(1);
$('#ys_comments').append('<div id="newbox" style="position:relative; width:450px; height:150px ; top:20px; left: 0px">Because you have selected an <b>international address</b>, you are now being routed to <span style="font-weight:bold;color:#FF8C00">Bongo </span><span style="font-weight:bold;color:#32CD32">International</span>, our third party international payment and shipping partner. Shipping, duties, and taxes will be calculated on the next page.<br /><span id="clockBongoInt" style="font-weight:bold;color:red;" ></span> </div>');
$('#newbox').append('</br><input id="valor" class="ys_primary" type="button" value="Send Order" >');
$("#valor").click(function(event){
BongoCheckout.init();
$("#BongoCheckoutForm").submit(); });
}
});
});
Upvotes: 1
Views: 106
Reputation: 61541
You can do it these two ways too:
if ( (!$("#shipping-country").val() == "US") && !$("#shipping-country").val() == "CA") && (una=="ok")) {
....
}
or
if ( !($("#shipping-country").val() == "US") || $("#shipping-country").val() == "CA") && (una=="ok")) {
....
}
Upvotes: 4
Reputation: 6896
Look at what your script is doing. It's saying "IF the user isn't in Canada OR the user isn't in the US, then do whatever it is you do to people who aren't in the US or canada"
However, that means that no matter what, one of those two checks will return true
. If the user isn't from the US, then that section will return true
. If the user IS from the US but isn't from Canada, then the second section will return true
. What you're meaning to do is check if they are in neither the US nor canada. Meaning, if($('#shipping_country').val() != 'US' && $('#shipping_country').val() != 'CA')
. "If they aren't in the US AND they aren't in canada"
Upvotes: 3