dotancohen
dotancohen

Reputation: 31491

Reconcile jQuery syntax inconsistency

I seem to have trouble remembering when to use <element> (with brackets) and when to use element (without brackets) when working with jQuery. Compare for example the adding and then removing of an option from a select:

$("#items").append( $('<option>', {value:"third", text:"Third"}) );
$("#items").find('option').remove();

The append method requires the brackets, whereas the find method does not. How to reconcile this inconsistency?

Thanks.

Upvotes: -1

Views: 67

Answers (1)

lanzz
lanzz

Reputation: 43178

You use the brackets when you are creating new elements, just like you use the brackets when you write HTML.

You don't use brackets when you are selecting existing elements, just like you don't use brackets when you select elements in CSS.

Upvotes: 4

Related Questions