Creating a custom color scheme for school events

I am currently studying coding in school and have just started my coding journey 3 months ago. This is my first post on a forum, so please be kind!

I am encountering an issue with the drag function.

For an extracurricular activity for school kids, I came up with the idea of creating a color synthesis project using a computer. My goal is to create a circle with a drag function and a square border around the circle for resizing purposes.

My main focus is on the JavaScript function rather than CSS. The opacity feature creates an additive color system for the color synthesis.

// JavaScript code for drag and resize functions...
// CSS styling for the webpage...
<nav class="nav">
  <a class="logo" href="#">Musiexplo</a>
  <div class="nav-buttons">
      <a class="nav-button" href="#">Connect</a>
      <a class="nav-button" href="index.html">New Search</a>
  </div>
</nav>
<body>
  <contain class="homesearch">
  <section class="sectionA row-col">
  <div class="container">
    <div class="square"></div>
    <div class="circle"></div>
  </div>

  <div class="container" >
    <div class="square"></div>
    <div class="circle" style="background-color: red;"></div>
  </div>
</section>
<section class="sectionB row">
  <form action="" method="post">
    <label for="style1"  ></label>
    <input type="text" type="subimt">
    <button class="btn btn-primary mt-3" type="submit">Style1</button>
    
    <label for="style2"  ></label>
    <input type="text" type="subimt">
    <button class="btn btn-primary mt-3" type="submit">Style2</button>
  </form>
</section>
</contain>

Answer №1

Your code is impressively clean, especially for someone new to coding. Great work!

One minor issue:

<input type="text" type="subimt">
is not valid because an input cannot have two types. Typically, it should be type="text" if you want users to be able to type in it. But I understand that this isn't the main focus of your question.

Below is the code with some additions, specifically console.log statements for each event function so you can track when they're being called. It seems that some functions are not executing properly, which might be causing your problem.

 // Your JavaScript code goes here
 /* Your CSS code goes here */
 <!-- Your HTML code goes here -->

I suspect the issue is related to how you're handling event listeners - adding and removing them multiple times. You should consider simplifying your approach by following these steps:

  • Attach all document event listeners just once outside of a function call
  • Avoid removing listeners; instead, use boolean flags inside them to control their behavior (which you're already doing to some extent)
  • Add event listeners to elements like circle only once, without removing them afterwards

Constantly adding and removing listeners adds unnecessary complexity. By making these adjustments, you'll streamline your code and improve its clarity.

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

Maintaining uniformity in Python by assigning and populating empty values for any missing HTML tags to ensure consistency across structures

Here are the HTML lines I currently have: <nonDerivativeTable> <nonDerivativeHolding> # First Holding <securityTitle> <value>Common Stock</value> </securityTitle> <ownershipNat ...

Automatically refreshing the canvas whenever the value of an HTML element is modified in Javascript

Below is the javascript code that is relevant <script> $.fn.ready(function() { var source = $('#source').val(); Meme('url1', 'canvas','',''); $('#top-line, #bottom-li ...

Can JavaScript be executed from the console while navigating between pages?

I am facing a challenge that I can't find any solutions for online or elsewhere. The task at hand seems straightforward. I am using the Chrome console and attempting to run Javascript code to navigate between pages with pauses in between. It feels lik ...

An error with code -4058 occurred when I tried to execute the command npm run start-rewired

When I start my React application using npm with the command npm run start-rewired, I encountered an error that looks like this: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="265654494c434552661608170816">[email p ...

What is the best way to ensure synchronous function execution in Angular?

Consider the following scenario: ServiceA is accessed by two components with inputs. ComponentA contains a textArea while ComponentB has a toggle button. Whenever these components are changed, ServiceA.save() is triggered, which subsequently makes an HTTP ...

Refreshing page with ReactJS internationalization (i18n) capabilities

Currently, I am utilizing react-i18next within my Reactjs application. An issue that I am encountering is that whenever I switch the language, the app reloads and always reverts back to the main route. Is there a method to redirect to the same page or ch ...

What are the possibilities of utilizing both Bootstrap 5 grid and Bootstrap 4 grid interchangeably?

After working with Bootstrap 4 in my Angular 12 project for some time, I decided to upgrade to Bootstrap 5 today. While I know that some properties have changed, I thought the grid system remained unchanged. I often use "Mix and Match" columns as describe ...

Reposition the div element on mobile devices using media queries

Hello, I'm facing an issue on mobile with my website: dev site Today, I implemented a dropdown menu with flags to allow language switching. However, the dropdown in mobile is appearing under the hamburger nav bar. I was thinking of using media querie ...

Efficiently Embedding Images into Canvas Elements

I am struggling with loading individual photos from an array into "canvas" tags. In my function, I generate multiple sets of "div" tags with a "canvas" tag inside them, but I'm facing difficulties in inserting images from an array into these "canvas" ...

An unusual visual anomaly (a small line appears) when hovering over a button in the web browser

There is a peculiar issue with some buttons on a web page where a strange line appears on the right side when hovering over the button. This problem only affects the first two out of three buttons, possibly because there is no fourth button to trigger the ...

Generate HTML on the fly using Node variables

Utilizing Node.js with Express as the server, I have implemented a feature where users can upload .CSV files containing data. This data is then parsed and stored in a main array made up of arrays, with each line representing one array element. Currently, I ...

What is the reason for Bootstrap requiring both a class name and an ID name for collapse functionality?

When implementing the collapse effect in Bootstrap, why do buttons need to refer to the data-toggle=collapse class and the data-target=#collapseExample ID of the panel for the effect? Wouldn't simply targeting the ID be enough to toggle its state? Co ...

Checking if the Cursor is Currently Positioned on a Chart Element in Word Addin/OfficeJS

I am looking for a way to determine if the document cursor is currently positioned inside of a Chart element using the Microsoft Word API. My current application can successfully insert text, but when I attempt to insert text into the Chart title, it ends ...

How can I convert a string containing integers into an int[] using javascript or jQuery?

I want to create a feature that allows users to input a list of IDs in a textarea, with the option to separate them by either whitespace or commas. After the user inputs the list, I need to convert it into an int[] array but also throw an error if the str ...

Attempt to generate a function in JavaScript that mirrors an existing one

How can I create a javascript function similar to loadAllOriginal, but with the value of the variable allEmployees being a list of employee objects (ID, FirstName, LastName)? I am attempting to implement this method in combination with autocomplete from ...

Creating a function in React to be passed as a prop

Recently, I was given the task of enhancing a user interface as a small challenge to help me dive into the world of programming. My goal is to incorporate a date picker into my search bar to allow users to filter their search results by selecting specific ...

Retrieving content dynamically using ajax

As I load comments via ajax, I start with 5 by default and allow the user to request more. My query is centered around the best approach. What is the optimal location to construct the HTML elements meant for display on the page? Would it be better to cr ...

Issues encountered with JavaScript when trying to use the jQuery API

I'm trying to fetch random quotes using a Mashape API, but when I click the button, nothing happens. I've included the JS and HTML code below. Can anyone spot an issue with the JS code? The quote is not displaying in the div. Thank you! $(' ...

Preserve the hidden div selection stored in an array even after the page is refreshed

I have a created a function that toggles between displaying one or all of the divs, based on user selection. Here is my script: <script type="text/javascript"> function switchdivs(theid){ var thearray= new Array("div1", "div2", "div3"); for(i=0; i&l ...

Exporting Typescript to Javascript files

I have created a sample TypeScript object with the following code: declare const S3 = "https://s3.amazonaws.com/xxx/icons"; declare const SVG = "svg-file-icons"; declare interface MyIcons { "image/jpeg": string; "image/jpg": string; } export const F ...