Shahzad Nasir
Shahzad Nasir

Reputation: 168

$(document).on("click") not working but $('selector').on("click") works why?

I am working with video.js and video.js library is loading via script tag I want to track click function on list item and update player-menu.

$(".vjs-menu-item").on("click" ,function(){ alert('selector');});

but not working this one

$(document).on("click",".vjs-menu-item", function() {alert('document'); });

Hence $(document) method only works on these items which added on same page i.e

<p id="vjs-menu-item"> click me </p>

Upvotes: 0

Views: 4019

Answers (2)

user6533277
user6533277

Reputation:

$(document).ready(function(){
    $("#vjs-menu-item").click(function(){
    alert('document');
  });
});

jsfiddle

Upvotes: 0

brk
brk

Reputation: 50291

Here the selector is id but you are referencing to a class

Change to this

$(document).on("click","#vjs-menu-item", function() {alert('documnet'); });

or use class instead of id in html

HTML

<p class="vjs-menu-item">Click me</p>

JS

$(document).on("click",".vjs-menu-item", function() {
    alert('document');
});

Upvotes: 4

Related Questions