Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if multiple elements with same class exist [duplicate]

Tags:

jquery

Wonder if there's any way to check if elements with the same class exists in a document.

For example:

<div class="panel">panel 1</div>
<div class="panel">panel 2</div>
<div class="panel">panel 3</div>

JS:

if ( $('.panel')[0] ) {
    console.log('exists')
}

.. but I want to check if MORE THAN ONE panel element exists, alteast 2.

like image 608
eozzy Avatar asked May 17 '14 12:05

eozzy


2 Answers

Try to use the length property to accomplish your task,

if($('.panel').length > 1) {
  console.log('yes, more than one element exist')
}
like image 122
Rajaprabhu Aravindasamy Avatar answered Nov 03 '22 23:11

Rajaprabhu Aravindasamy


if ( $('.panel').length >= 2 ) {
    console.log('exists')
}

This should work

like image 3
Fonzy Avatar answered Nov 03 '22 23:11

Fonzy