0

I'm just wondering about the difference between the jQuery functions ".trigger('play')" and ".play()". I want my website to play a sound when you press a navigation button. Its supposed to the sound of a HTML5 -tag.

It works when I use this jQuery code:

$('nav ul li a').click(function(){
    $('#soundFX').trigger('play');
});

But not this:

$('nav ul li a').click(function(){
    $('#soundFX').play();
});

Thank you for answering!

  • Refer to [this](http://stackoverflow.com/questions/4646998/play-pause-html-5-video-using-jquery) SO question. `play` is not a function of jQuery, but a regular DOM function. – Bikonja Aug 18 '13 at 10:24

2 Answers2

3

Play is not a jQuery method, it is a function of the DOM element. You would therefore need to call that on the Dom Element itself as in:

       document.getElementById('soundFX').play();

When you call the trigger method on a jQuery object you are triggering native events on that element and in this case you are triggering the native play method of the element selected by your jQuery selector. Hope that helps. R.

Rob Schmuecker
  • 8,934
  • 2
  • 18
  • 34
1

I just noticed that .trigger('play') will cause the play handler being called twice, but .play() will cause only once.

demo

lzl124631x
  • 4,485
  • 2
  • 30
  • 49