1

I have HTML like that:

<div class="mydiv">
       <script>
           //my script
       </script>
</div>

I want now check mydiv class has script or not. How can I do this with Jquery?

Hai Tien
  • 2,929
  • 7
  • 36
  • 55

3 Answers3

13

first go yo div with class .mydiv then find script then check its length

   if(  $(".mydiv").find("script").length)
     {    
       alert("exist");
     }
Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55
5

You can use Descendant Selector (“ancestor descendant”) with div. Using length with selector will show how many script (tag) element found in the container that is div with class mydiv

if($('.mydiv script').length)
{
   //script tag found
}

Description: Selects all elements that are descendants of a given ancestor.

Adil
  • 146,340
  • 25
  • 209
  • 204
0

It would be:

$(function(){
  if($('.mydiv').children('script').length>0){
    // have script tab.
  }
});
Ringo
  • 3,795
  • 3
  • 22
  • 37