0

I'm trying to see if there are any elements inside one div, and also, if there is more than one element in another div. If so, I want some things to happen. I've gotten errors running my script (not a valid property) and more of my js script doesn't get executed if it's below this. so, other than moving this script down my js page, what are my options? My logic seems fine, just the error bothers me.

var firstDiv = $('.firstDiv');
var secondDiv = $('.secondDiv');
if( (secondDiv).html().length > 0 || $(firstDiv).html().length > 1 ){
    make stuff happen
}
james
  • 707
  • 2
  • 6
  • 13

2 Answers2

0

As MinusFour mentioned, make sure that your selectors are correct. If the JQuery object is empty, the html() method will return null. There is no length property on null.

You can see how many elements the JQuery object wraps using its length property:

$('.secondDiv').length // should be greater than 0
Ilia Choly
  • 18,070
  • 14
  • 92
  • 160
0

try

if((secondDiv.html() && secondDiv.html().length > 0) || ($(firstDiv).html() && $(firstDiv).html().length > 1)){
make stuff happen
}
Amit
  • 427
  • 4
  • 16