Looking for a drum set with clickable buttons? Having trouble changing the background color in CSS/HTML? Wondering how to map keyboard keys to HTML buttons?

Behold my HTML creation:

<H1> <center> EPIC GUITAR JAM  </center> </H1>
    <img class="guitar" src="guitar.jpg" />
    <button class="strum" onclick="Strum()"> Strum Chord </button>
    <button class="pluck" onclick="Pluck()"> Pluck String </button>
    <button class="slide" onclick="Slide()"> Slide Solo </button>
    <button class="bend" onclick="BendNote()"> Bend Note </button>
    <button class="harmonics" onclick="NaturalHarmonics()"> Natural Harmonics </button>
    <hr />

CSS Styles

.guitar {
    position:absolute;
    margin-top:2em;
    margin-left:30em;
}
.slide {
    position: absolute;
    margin-top:15em;
    margin-left:75em;
}
.bend {
    position: absolute;
    margin-top:15em;
    margin-left:48.5em;
}
.harmonics {
    position: absolute;
    margin-top:23em;
    margin-left:42.4em;
}
.pluck {
    position: absolute;
    margin-top:23em;
    margin-left:78em;
}
.strum {
    position: absolute;
    margin-top:21.5em;
    margin-left:54.5em;
}

Javascript Functions

function Strum() {
var strum = new Audio("strum-acoustic01.wav");
strum.play();
}

// Additional functions for Pluck, Slide, BendNote and NaturalHarmonics

If you're struggling to add background color using CSS or inline styles, make sure your CSS file has proper syntax and is linked correctly in your HTML document. For assigning keyboard keys to HTML buttons, you'll need JavaScript event listeners. Feel free to ask for help on Stack Overflow or try online tutorials for guidance. Keep experimenting and learning - it’s all part of the process! :)

Answer №1

To link a keyboard event to the Document Object Model (DOM), you must connect the event handler to the Document.

An easy way to achieve this is through jQuery:

$( "document" ).keypress(function( event ) {
  if ( event.which == 13 ) { // keycode 
     event.preventDefault();
  }
});

https://api.jquery.com/keypress/

The JavaScript-only approach (without using jQuery):

function addEvent(element, eventName, callback) {
    if (element.addEventListener) {
        element.addEventListener(eventName, callback, false);
    } else if (element.attachEvent) {
        element.attachEvent("on" + eventName, callback);
    }
}

addEvent(window, "keydown", Keypress);
addEvent(window, "keyup", Keyoff);

JavaScript equivalent of jQuery's keyup() and keydown()

Key Code

You can check the keycode at interactively.

One more thing

If you have two questions, it's best to separate them into two distinct inquiries.

Answer №2

Forget about setting "background-color",

All you need is to set background:

For example:

.custom-class{
    background: #00ff00;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Refresh TR when clicked

I have a HTML table that lists items from SQL, using <tr> and <td>. The table is housed within a div that is refreshed every 30 seconds with jQuery AJAX (hence the unique id on the div). Here is the HTML code: function auto_load() { $.aj ...

Specify the return type based on specific parameter value

I'm facing a situation where I have two definitions that are identical, but I need them to behave differently based on the value of the limit parameter. Specifically, I want the first definition to return Promise<Cursor<T>> when limit is g ...

Tips for avoiding unintended single clicks while double clicking in React

How can I make a function trigger on both single click and double click events when working with a video element in React? Currently, the single click function is also being called when double clicking. I am seeking a solution to this issue. Below is the ...

Creating a text file while in a suspended state within the event handler on Windows 8 using HTML5

When the suspend event is triggered inside the winjs.application.oncheckpoint event handler, I am attempting to write a text file. The content of the file is my object in JSON format. Below is the code snippet: applicationData.localFolder.createFileAsync( ...

javascript cannot utilize html reset functionality

My drop down menu includes an onChange event that triggers a JavaScript method. However, when I select a new value and then click the reset button, the dropdown reverts back to its original value but the onChange event does not fire. <select onChange= ...

Issues with the creation of an AngularJS table are causing functionality to malfunction

2021 UPDATE: Note: This question was drafted when I was still learning HTML and JS. If you are seeking assistance for an issue, this might not be very useful as my code was messy. Sorry for any inconvenience caused. The question will remain posted in acco ...

What is the best way to create a fully clickable navbar item for the Bootstrap dropdown feature?

I'm struggling to make a button in a navbar fully clickable for the dropdown to open. Even when I try adding margin instead of padding, it only makes things worse. Can someone help me figure out what mistake I'm making here? Essentially, my goal ...

Fetching Information from Firebase and Populating Dropdown Menus on a Website

Displayed below is a code snippet from a .JSON file stored in a Firebase database. "questionnaires" : { "ORANGE" : { "location" : "ny", "major" : { "engineering" : { "ECE" : { "3rd sem" : { "2018 ...

Despite locating the button, Protractor still encounters difficulties when attempting to click on it

I've been having trouble clicking on a button using Protractor. The issue is that even though the driver can locate the element, it still won't click on it. Any assistance would be greatly appreciated. Thank you in advance. Here is the HTML for ...

Can CSS3 animations be executed sequentially for each child element?

Have you ever tried creating a CSS3 animation in HTML? You can check out an example here. <div class="parent"> <a href="#">First</a> <a href="#">Second</a> <a href="#">Third</a> </div> With the ...

Creating a comparison display using ng-repeat

I have a JSON file that contains revision and history information about a modified entity, including its old and new values. The diff is generated using the jsondiffpatch library and I have parsed it further to create the final JSON format for rendering. ...

Limit image size in Outlook when using Mailchimp

I'm currently working on coding a Mailchimp template for a client and I've run into an issue with image dimensions. The problem arises when images exceed the width of the template (usually 600px), as they are displayed at their original size in ...

ng-repeat and $scope problem

I am having an issue with my page where I display 3 images in a row using Ng-repeat. When I click on any image, it only shows the first image that was displayed in that particular row. Template: <div id="galscrolldiv" class="row" ng-repeat="image in i ...

Discover how to access and manipulate JSON files in an Angular application using

Currently, I am diving into learning TypeScript with Angular and I'm interested in reading a JSON file. The structure of my JSON file is as follows: { "nb": "7", "extport": "1176",, "REQ_EMAIL": ...

The console is not displaying the data from the useForm

I am working on a project to create a Gmail clone. I have implemented the useForm library for data validation. However, when I input data into the form and try to print it to the console, nothing is being displayed. const onSubmit = (data) => { ...

What is the best way to have dual backgrounds displayed on a single webpage?

Looking to recreate the background design featured on this website: www.zucoffee.com. Does anyone have any tips? In addition, I'm curious about how to achieve that curved separation effect. I understand it involves two divs, but how did they manage t ...

Retrieve JSON data from a PHP script to be used in an Angular scope

I am attempting to retrieve JSON data from a PHP file to use in an Angular controller. I have used json_encode(pg_fetch_assoc($result)); within the PHP file and when I check with console.log($scope.contents); in the Angular controller, the JSON data is ret ...

What is the best way to confine the child elements within a parent element using CSS?

Check out my CSS and HTML code here: https://jsfiddle.net/z2cc11yk/ Here's the HTML: <div class="progressbar-container"> <div class="progressbar-text">125%</div> <div class="progressbar-progress" style="background-color: red; wi ...

Creating a stunning HTML 5 panorama with GigaPixel resolution

Interested in creating a gigapixel panorama using HTML 5 and Javascript. I found inspiration from this example - Seeking advice on where to begin or any useful APIs to explore. Appreciate the help! ...

Issues with Jquery/AJAX function not responding to onChange Event

I am currently working on a project where I need to populate a dropdown menu based on the value selected from another dropdown. However, I am encountering an issue with the function that I am trying to call in the onChange event of the select tag. This is ...