Neha Dangui
Neha Dangui

Reputation: 647

Detect HTML5 video tag

I had a query regarding HTML5 video tags. I wanted to know if there is any way to check if the text to be added in the body tag contains a HTML5 video tag using jQuery or javascript.

<video>
     <source src="demo.mp4" type="video/mp4" />
     <source src="demo.ogv" type="video/ogg" />
</video>

I cannot give any id or class to the video tag. I mean can we have something like

 if($('body').contains('<video>')){
      // do something
    }

Thanks in advance.

Regards,

Neha

Upvotes: 1

Views: 2870

Answers (5)

Sunny Sharma
Sunny Sharma

Reputation: 4934

you can try this:

if($('video').length>0){
   console.log('Video tag exists');  // do operations.
}

Upvotes: -1

Milind Anantwar
Milind Anantwar

Reputation: 82231

Use:

 if($('video').length===0){
   //video not found
  }

Upvotes: 3

Sterling Archer
Sterling Archer

Reputation: 22395

You don't need jQuery for this, you can use getElementsByTagName()

var x = document.getElementsByTagName("video");
if (x.length) {
    //do code if element(s) are present
}

Upvotes: 6

Prashobh
Prashobh

Reputation: 9542

if($("video").is(':visible')){
//do somthing
}

You can find HTML5 Video events here

http://www.w3.org/2010/05/video/mediaevents.html

Upvotes: 0

dhana
dhana

Reputation: 6525

Try this,

if($( "body:has(video)" ).length)

Upvotes: 0

Related Questions