Reputation: 10230
hey guys i was just going through the source if modal.js and came across the following line :
if (!that.$element.parent().length) {
}
i am new to JS and Jquery in general , what i figure is the not operator is being used here and basicall we are getting the parent element of $element and then accessing the length property . but i still don't quite get the practical usage of this condition .
This condition can be seen on line 79 , on git too .
Thank you.
Alex-Z .
Upvotes: 0
Views: 50
Reputation: 21766
It checks if the given element already exists in the the DOM. This logic ensures it will not be added twice:
if (!that.$element.parent().length) {
that.$element.appendTo(that.$body) // don't move modals dom position
}
Upvotes: 1
Reputation: 2290
That is basically checking if such an element exists. Every JQuery selector implicitly returns a list of elements, which can be empty. If length is 0, this will evaluate to false in a boolean context, so ! in front will make this condition true if the element does not exist (element list is empty).
Upvotes: 1