Alex Knopp
Alex Knopp

Reputation: 935

jQuery if statement syntax check

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.

Upvotes: 0

Views: 57

Answers (2)

Tushar
Tushar

Reputation: 87203

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

Upvotes: 3

Rajan Goswami
Rajan Goswami

Reputation: 769

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

Upvotes: 1

Related Questions