Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6960155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:21:57+00:00 2026-05-27T15:21:57+00:00

This a small portion of the javascript code I used for an HTML5 music

  • 0

This a small portion of the javascript code I used for an HTML5 music player. I was wondering if you guys can help me build a HotKey to toggle the Play/Pause button using the Space key. Any ideas?

CODE:

   $('.trackslist li').live('click', function(event) {

    var $track = $(this),
        $player = $track.closest('.player'),
        trackId = $track.data('sc-track').id,
        play = $player.is(':not(.playing)') || $track.is(':not(.active)');

    if (play) { onPlay($player, trackId); }else{ onPause($player); }

    $track.addClass('active').siblings('li').removeClass('active');

    return false;

  });

    $('.next').live('click', function(event) {
        $player = $(this).closest('.player');
        onNext($player);
    });

    $('.prev').live('click', function(event) {
        $player = $(this).closest('.player');
        onPrev($player);
    });

An idea I found:

document.onkeydown = function(e){

    var ev = isIE?event:e;
    if(ev.charCode && ev.charCode == 32)
        player.playPause();
    else{
        switch(ev.keyCode){
            case 32:
                player.playPause();
                break;
            case 39:
                player.nextSong();
                break;
            case 37:
                player.prevSong();
                break;
            case 38:
                player.volumeInc();
                break;
            case 40:
                player.volumeDec();
                break;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:21:57+00:00Added an answer on May 27, 2026 at 3:21 pm

    In your code for the keydown event on the document element, the player variable is not set to anything.

    Also jQuery normilizes the e.keyCode and e.charCode properties with e.which but that is only if you bind to the event using jQuery:

    $(document).on('keydown', function (event) {
        var key    = event.which,//get charCode of event
            player = $('audio');//get the player (I'm assuming it's an `audio` tag)
    
        switch(ev.keyCode){
            case 32:
                player.playPause();
                break;
            case 39:
                player.nextSong();
                break;
            case 37:
                player.prevSong();
                break;
            case 38:
                player.volumeInc();
                break;
            case 40:
                player.volumeDec();
                break;
        }
    
    });
    

    I noticed that trackId = $track.data('sc-track').id probably isn’t actually getting the ID of the $track element, try one of these:

    trackId = $track.data('sc-track')//this will get the `data-sc-track` attribute for this element
    

    OR

    trackId = $track[0].id//this will get the ID of this element
    

    One last note, .live() has been depreciated as of jQuery 1.7. You should be using .delegate() if you are using an older version and .on() if you are using jQuery 1.7 or greater.

    Source: http://api.jquery.com/on

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is a small portion of the code: @interface BOOL isCarryingmallet; @implementation -(BOOL)isCarryingWeapon {
I have this small code library that I'm considering releasing into Open Source. I
I've found this small code that sends email to gmail users. I'd like the
Work on this small test application to learn threading/locking. I have the following code,
Below is a small portion of the source code where fclose is causing error?
I am considering porting a small portion of the code in a C# project
This is a very small portion of a large project... These are typedefs defined
I've got this small question - given a bitmask of weekdays (e.g., Sunday =
I have this small SQL query. SELECT a.`id` , a.`title` , a.`date` , (
I have this small class called City that simply holds some information about a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.