Why does the following code return an empty array as a result ([]
)?
$('#non-existing-id').first();
I thought that it should return null
or undefined
.
How can I check for the success then? I don't see anything about it in the documentation.
Why does the following code return an empty array as a result ([]
)?
$('#non-existing-id').first();
I thought that it should return null
or undefined
.
How can I check for the success then? I don't see anything about it in the documentation.
Why does the following code return an empty array
It doesn't. It returns a jQuery object containing only the first match.
If there are no matches, that jQuery object contains zero elements.
I thought that it should return null or undefined.
No, the documentation says it returns a jQuery object.
How can I check for the success then?
Test the number of matches using length
.
if ($('#non-existing-id').length > 0)
$('#non-existing-id')
returns an empty array since it found zero matches.
.first()
returns zero results since it doesn't find any result in an empty array.
$('#non-existing-id').eq(423424);
also returns an empty array []
as example.