JQuery.find () returns an object, even if there is no corresponding child in the DOM

I am trying to find the element with the identifier '' that is inside the element '', and therefore is its child element.

I use the $ .find method to do the search.

If a child is found, I would like to perform some actions, and if the child is not found, I would like to do different things.

However, although I know that such a child does not exist, the jQuery.find method reports an object that I am not sure from checking in the Clock window what it is.

Here is the relevant code snippet:

function CreateResourceKeyTextBox(resourceKeyId, editMode) {
    var resourceKeyTableCell = $("#tdKeyResourceKeyId" + resourceKeyId);

    var resourceKeyNameTextBox = null;

    var alreadyExistingResourceKeyNameTextBox = resourceKeyTableCell.find('#txtResourceKeyName' + resourceKeyId);

    if (alreadyExistingResourceKeyNameTextBox != null && typeof alreadyExistingResourceKeyNameTextBox != "undefined") {
        resourceKeyTableCell.html('');
        resourceKeyNameTextBox = alreadyExistingResourceKeyNameTextBox;
        resourceKeyNameTextBox.css('display', 'block');
        resourceKeyNameTextBox.appendTo('#tdKeyResourceKeyId' + resourceKeyId);
        resourceKeyNameTextBox.css('width', '96%');
    }
+4
source share
4 answers

jQuery , DOM.

, 0, :

if (alreadyExistingResourceKeyNameTextBox.length ...
+7

alreadyExistingResourceKeyNameTextBox.length != 0,

+2

jquery find jquery, css.

css- - , jQuery , , . .get :

var elems = $.find(css_selector).get()

DOM, jquery, ,

var elems = $.find(css_selector).get()
if(elems.length === 0){
    //array is empty
}else{
    //array is not empty

}

jquery , , jquery , , css DOM . , . -, .

+2

If the object is not found using the jquery.find () method, it always returns an empty array. if you get something else, you need to check your DOM. You can always check the length of the result, i.e. Result.length> 0 || result.length === 1, depending on your needs

+1
source

All Articles