Determining whether jQuery has not found any element

后端 未结 4 1322
执笔经年
执笔经年 2020-12-07 21:17

I\'m using jQuery\'s selectors, especially id selector:

$(\"#elementId\")...

How should I determine whether jQuery has found the element or

4条回答
  •  一个人的身影
    2020-12-07 22:03

    Futuraprime is right but you can shorten your syntax by doing the following:

    if ($("#id").length) {
       //at least one element was found
    } else {
       //no elements found
    }
    

提交回复
热议问题