susan
susan

Reputation: 382

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 time.

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')); 
}    
else
{
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'));      
}
}

Thank you.

EDIT: To clarify, I do understand 'else' means either one or the other. The problem here is that even if the condition is met for the 2nd if statement, it doesn't execute. I can only get a reaction from one if statment at a time using the above code or any similar variation that I have tried.

Upvotes: 0

Views: 98

Answers (4)

Stefanos Vakirtzis
Stefanos Vakirtzis

Reputation: 448

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')); 

    }

Upvotes: 2

Abraham Hamidi
Abraham Hamidi

Reputation: 13789

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.

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385114

That is what if and else mean.

if (A) {
   B
}
else {
   C
}

If A, then B, otherwise C.

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

Upvotes: 1

laaposto
laaposto

Reputation: 12213

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

Upvotes: 1

Related Questions