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

Ensure that the child element completely fills the parent element while maintaining a padding around the edges

How can I create a child div that fills 100% of the parent's width and height, including padding? I've tried using box-sizing = border-box, but it isn't working. I also attempted adding the box-sizing property to all elements with *, but tha ...

Sudden slowdown due to Jquery error

I am encountering an issue with the Jquery registration validation. I am struggling to display the error message if a name is not filled in. jQuery(document).ready(function () { $('#register').submit(function () { var action = $(thi ...

Navigate to a unique component

I am attempting to navigate to the location of a custom component, but the reference to it is not returning a valid HTML node. The function "goToContactUs" should execute this action. What would be the most effective approach to accomplish this? class H ...

Modifying the geometry of a plane in Three.js

Currently working on a simple terrain editor. When the mouse is clicked, I want the selected face to move up. The intersection is functioning well, and I am attempting to adjust the geometry in this manner: var intersects2 = ray.intersectObjects([ ...

The type 'IProduct' cannot be assigned to type '[]'

Looking for help with dispatching events between parent and child components. Interfaces: export interface IProduct { id: string; name: string; price: string; image: string; inStock: number; fastDelivery: bo ...

Leveraging Gatsbyjs to Integrate GraphQL Data with Material UI Library

Just starting out as a Frontend Developer and currently learning Gatsbyjs along with the Material UI library. I'm working on creating a new page using the Material UI Gatsby Starter code available here. However, I've encountered an issue when tr ...

Ajax is not able to trigger a POST request to a PHP form

My form is not posting for some unknown reason, despite everything else on my server functioning properly. Below is the code snippet in question: PHP <?php if(isset($_POST['field1']) && isset($_POST['field2'])) { $data ...

Vue's intelligent element loading feature ensures that elements that are not displayed are delayed

My Vue gallery component includes a lightbox feature defined by the following code: <div id="lightbox" class="modal" v-if="photo !== null" v-show="showModal" @click.self="closeModal"> <div clas ...

The property of undefined map cannot be read

import React from 'react'; import { Card, Text } from "react-native-paper"; import { SafeAreaView, } from "react-native"; class HackerNewsClone extends React.Component { constructor(props) { super(props); this.sta ...

Leveraging trustAsHTML with an array of object elements

I'm facing a challenge in passing an array of objects from an Angular Controller to the ng-repeat directive. The objects within the array have multiple properties, some of which may contain HTML that needs to be displayed using the ng-repeat. I' ...

What is the proper way to employ if and else if statements within Angular2?

Here's a question that has been duplicated on my How to utilize *ngIf else in Angular? post! ...

Div over which scroll editing is performed

I want to develop a website where my title div remains fixed in one position as I scroll down. However, the issue arises when I set it to be fixed because my navigation button on the left disappears and I'm unsure how to keep it in place. // JavaSc ...

Problem with spacing in Bootstrap horizontal layouts

My project is currently empty except for a code snippet I copied from Bootstrap's horizontal gutters page. Unfortunately, I am not seeing any horizontal gaps as expected. Why could this be? It seems that only vertical gutters are working... Click her ...

distance between the top of the window and divs within an array

I'm having trouble calculating the distance of divs within an array from the top of the window. Whenever I try to run the code, I keep getting an error message saying "player.offset is not a function". Can someone please point out where I am making a ...

How can we reduce the use of !important in SCSS when working with React and Material UI?

In my current project, I am developing a Select component in React with Material UI. The CSS styling for the component is managed through an external SCSS sheet imported into the script file. While working on restyling the component, I found it challengin ...

Add a fresh item to a list using jQuery

Attempting to add a new list item using jQuery. In this scenario, the process works well, but when attempting to retrieve the value of an input field using .val(), no list item is created. http://www.example.com Code snippet: $(document).ready(functi ...

Looking to transfer zip files from a React user interface to a Node.js server backend, where I can then save the folder to a specific directory on my system

Frontend I am currently working on a feature that involves uploading a zipped folder containing CSV files from the React UI input field. import React, { useState } from "react"; import axios from "axios"; function App() { const [uplo ...

Utilizing HTML and JavaScript to dynamically switch stylesheets based on the width of

When it comes to using stylesheets for mobile and non-mobile devices, there is a need for different approaches. The idea of comparing browser height and width in JavaScript seems like a good one, but the implementation may not always work as expected. ...

Step-by-step guide to generating a Paypal Button using Vue 3 script setup

After going through the PayPal Developer Docs, I'm still struggling to understand how to integrate the PayPal Button into Vue.js. The code examples provided are unclear, and I can't determine if it's related to Vue 2, Vue 3, or even Angular. ...

Achieve a floating right alignment of an unordered list navigation using CSS without changing

I am currently implementing a jquery navigation system which can be found at this link: http://css-tricks.com/5582-jquery-magicline-navigation/ My goal is to have the navigation bar float to the right without changing the order of items (e.g. home, contac ...