Does jQuery always return array when selecting element (of course if at least one element exists)? Example:
$('#Myelement')
$('div')
$('tbody')
What if the selector is an ID? What if the selector is an element but has only one occurrence?
The jQuery function always returns a jQuery object (that is based on an array), even if there are no elements that matches the selector.
That way you can always call a method that is supposed to affect the elements found, even if there are no elements that matched. If the jQuery object contains no elements, it will simply do nothing.
If you need to know if a jQuery object contains any elements, you can use the length property.
$('td')[0].html('hi'), it gives me an exception, it says that index has no .html method, meaning the elements on the array returned by jQuery are not part of the jQuery object. Am I wrong?[0] to get an element out of the jQUery object, it's a plain DOM element that you can't use jQuery methods on. You can use the first method to limit the result to the first element: $('td').first().html('hi'), or you can wrap the element in a jQuery object: $($('td')[0]).html('hi') (but that will cause an error if the selector found no elements)..length and .splice()