0

Whats wrong with this statement.

if (data.bookingtype = 'Corporate') {
    jQuery('#rentalprice').val(data.baserentalprice);

} else if (data.bookingtype = 'Groups') {
    jQuery('#priceperperson').val(data.baserentalprice);

} else if (data.bookingtype = 'Leisure') {
    jQuery('#priceperperson').val(data.baserentalprice);

}

I am trying to populate a field based on the value of the bookingtype variable sent back from my ajax request.

If bookingtype is Corporate then populate the #rentalprice input, else if its Groups or Leisure then populate the #priceperperson input. At the moment it always populate the #rentalprice input.

2
  • 5
    == or === (equality operator) instead of = (assignment operator) Commented Jul 24, 2015 at 11:14
  • Duplicate of stackoverflow.com/questions/16250235/… and what not Commented Jul 24, 2015 at 11:20

2 Answers 2

3

You are assigning value to the variable instead of comparing. So, the first if is always evaluated to true and the statement of if block will be executed.

Use equality operator(==) or strict equality operator(===) instead of assignment operator(=).

if (data.bookingtype == 'Corporate') {
    jQuery('#rentalprice').val(data.baserentalprice);
} else if (data.bookingtype == 'Groups') {
    jQuery('#priceperperson').val(data.baserentalprice);
} else if (data.bookingtype == 'Leisure') {
    jQuery('#priceperperson').val(data.baserentalprice);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Why Downvote? Please add comments here. Can something be improved?
1

You are using an assignment operator = instead of equality operator == or ===. The code should be as follows:

JQUERY

if (data.bookingtype == 'Corporate') {
    jQuery('#rentalprice').val(data.baserentalprice);
} else if (data.bookingtype == 'Groups') {
    jQuery('#priceperperson').val(data.baserentalprice);
} else if (data.bookingtype == 'Leisure') {
    jQuery('#priceperperson').val(data.baserentalprice);
}

Comments

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.