Skip to content Skip to sidebar Skip to footer

Issue With Jquery 'else'

I am not sure what I'm doing wrong but only the first half of my script is being picked up. I've tried various combinations and can only get one of the if statements to work at a t

Solution 1:

Try :

if ($('#LocalDelivery').is(":checked") == false) {
    $('#datepicker').attr("value", "");
    $('#LocalDate').val($('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').val());
    $('#LocalDate').attr('name',
    $('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').attr('name')); 
}    
if ($('#StandardShip').is(":checked") == false) {
    $('#LocalDate').attr("value", "");
    $('#datepicker').val($('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').val());
    $('#datepicker').attr('name',
    $('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').attr('name')); 

}

You can also change .is(":checked") with .attr("checked","checked").

EDIT: I just noticed that you are trying to implement two specific values in the same element #datepicker and thats what must be causing your first conditional not to get picked.

What i mean is that when both checboxes are not checked you are trying to put TWO values in ONE element #datepicker.

The above means that the logic of your code is wrong.You could instead for example have only one checkbox and do the following:

if ($('#LocalDelivery').is(":checked") == false) {
        $('#datepicker').attr("value", "");
        $('#LocalDate').val($('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').val());
        $('#LocalDate').attr('name',
        $('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').attr('name')); 
    } else {
        $('#LocalDate').attr("value", "");
        $('#datepicker').val($('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').val());
        $('#datepicker').attr('name',
        $('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').attr('name')); 

    }

Solution 2:

Try

if (!$('#LocalDelivery').is(":checked")) {
    $('#datepicker').attr("value", "");
    $('#LocalDate').val($('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').val());
    $('#LocalDate').attr('name',
    $('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').attr('name')); 
}    
if (!$('#StandardShip').is(":checked")) {
    $('#LocalDate').attr("value", "");
    $('#datepicker').val($('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').val());
    $('#datepicker').attr('name',
    $('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').attr('name')); 

}

If you're using else, the second if will be executed if the first one returns false, so what you had is what you should've expected. The first if returns true, so the second one is ignored.

Solution 3:

And that is how it is supposed to work. If first half is true then the else block would not be executed. Every time only one block will be executed. Never both

Solution 4:

That is what if and else mean.

if (A) {
   B
}
else {
   C
}

If A, then B, otherwiseC.

If you didn't intend the "otherwise" portion of this little equation, then do not use else!

Post a Comment for "Issue With Jquery 'else'"