Determining if a div contains a table element in JQuery

For my accordion, every time the user clicks on the title, I want to see if the accordion content contains a table, and if not, generate it and add it to the accordion content. But how do you check if the current element contains a specific element type?

   <div class="a-header"> 
      <ul><li></li></ul>
    </div>
    <div class="a-content">
    </div>

    $('.a-header').click(function(){
       var currentHeader = $(this).next();
       //if currentHeader contains 'table'
});

      

Thanks.

+2


a source to share


3 answers


You are almost there:



 $('.a-header').click(function(){
   var currentHeader = $(this).next();
   if (currentHeader.find('table').length > 0) {
       //do stuff
   }
});

      

+4


a source


both are good, but faster selector



$(this).next(':not(:has(table))');

      

0


a source


$('.a-header').click(function() {
  if (!$(this).next().has('table').length) {
    // CREATE THE TABLE
  }
});

      

0


a source







All Articles