0

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.

5
  • 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. You're contradicting yourself. You say you understand that only one can work, but then you say you're surprised by it. Commented Jan 5, 2014 at 23:08
  • By the way, the language is JavaScript. else is a Javascript construct. jQuery is just a JavaScript library you're using. Commented Jan 5, 2014 at 23:11
  • The code inside the second if-statement will execute only if #LocalDelivery is NOT checked AND #StandardShip is NOT checked. Is that what you want? Could you clarify when you want the code in the second if-statement to execute? Also, are these radio buttons or checkboxes? Commented Jan 5, 2014 at 23:31
  • Hi John, thanks so much for your response. I guess I wrote this all wrong. What I want is for the first if to execute only if #LocalDelivery is checked and the 2nd if to execute only if StandardShip is checked. Commented Jan 6, 2014 at 0:08
  • Lightness-I guess I'm not being clear but other people seem to understand what I'm asking so if you would like to help, please see the other responses and my comments. Thank you. Commented Jan 6, 2014 at 0:09

4 Answers 4

2

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

    }
Sign up to request clarification or add additional context in comments.

8 Comments

Stefanos thank you very much for your response. As I said in my reply to Man of Snow, "This is one version I tried but went ahead and gave it another shot. Unfortunately it only picks up the last if statement so still not working." Any other ideas?
If you would like go and use else if and tell me the result. Also is there any other code involved in this conditonals that you havent posted here?
Hi Stefanos! I tried your above else if code and it's just picking up the first if statement. I have a lot of other script on this page but none that should affect this and there are no issues in the console. ???
Check my last edit on my answer and please tell me if thats the case.
I agree, my logic is totally wrong. I should probably go about this another way altogether. In the meantime I did attempt your code and it isn't working in my current setup. I think it might be just the wrong way to do this.
|
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.

3 Comments

Thank you for the response! This is one version I tried but went ahead and gave it another shot. Unfortunately it only picks up the last if statement so still not working.
F12 and check to see if there are any red X's sometimes you don't spell something correctly and it bricks part of the code
Chitowns I have no issues in the console. Thanks for your help! Any other ideas?
1

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

1 Comment

Sorry I was unclear. I understand this. What I am saying is when the other condition is met it doesn't pick it up. Only one of the if statements are ever functional at one time.
1

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!

3 Comments

Hi, per my comment to laaposto, "Sorry I was unclear. I understand this. What I am saying is when the other condition is met it doesn't pick it up. Only one of the if statements are ever functional at one time."
@susan: Sorry but that doesn't make any sense. Did you perhaps miss that your second if is inside the else from the first if?
Hello Lightness..I am sure I missed a lot or I would have figured this out without help. ;-) If you can post an answer to explain that would be most helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.