I am seeking advice on how to incorporate JavaScript to adjust slider values and add numerical values or prices. Any suggestions would

Seeking assistance with a unique project: I am currently developing a calculator that allows users to utilize sliders to select the best option for themselves across various categories.

My experience with JavaScript is limited, so I decided to reach out here. I aim to incorporate price values into each label so that it's possible to display the total pricing once the user clicks on "See Pricing."

For instance, selecting "High end" (50€) under the Style of design category and "Protein2" (5€) under the Database integration category should result in a total pricing of 55€ upon pressing the "See Pricing" button.

Therefore, my question is how can I associate different prices with these labels for user selection? And what would be the most efficient way to do this for future code modification by others?

Visual representation of my project because describing it verbally is not my forte

Progress thus far:

HTML:

<form id="divination-form">
   <h2>Project price calculator</h2>
   <h4>Use the sliders below to see what your project may end up costing!</h4>
   <div class="inputbox">
      <label for="slider1">Style of designㅤ</label>
      <input type="range" min="1" max="5" id="slider1">
      <div id="slider-label1"></div>
   </div>
   <div class="inputbox">
      <label for="slider2">Database integrationㅤ</label>
      <input type="range" min="5" max="8" id="slider2">
      <div id="slider-label2"></div>
   </div>
   <button type="submit" class="btn btn-primary" id="pricing-btn">
   See Pricing
   </button>
   <div id="pricing-result" class="pricing-result-container"></div>
</form>

JAVASCRIPT:

// Javascript code goes here

This results in you only being able to see the sum of the prices for a split second after clicking on "See Prices" button, which I am trying to find a solution to- but at the moment I am more concerned with a feeling that the code might not be as convenient or clean looking if I am planning to add a lot more sliders (which means adding more labels, prices and so on)..... So, is there a better solution to all of this?

Answer №1

Welcome to the StackOverFlow community! 🥳 I empathize with your issue regarding the page refreshing when clicking the submit button.

This occurs because the form triggers a submission action, causing the page to refresh in order to send data to the backend. To resolve this issue, you can prevent the default behavior using JavaScript:

document.getElementById("divination-form").addEventListener("click", 
   function(event) {
    event.preventDefault();
});

Click here for further details

Another common problem that may arise is the following error message:

Uncaught TypeError: Cannot read properties of null

This error typically occurs when the element with the id "total-price" is missing from the HTML code. Ensure all names, classes, and ids are correctly included in the HTML before referencing them in your JavaScript code.

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

A comprehensive guide on how to find keywords within an array and then proceed to make all text within the parent div tag stand

I am currently looking for a webpage that displays a list of products based on keywords from an array. Once it detects any word in the array, it highlights it with a red background - everything is working smoothly so far. However, I now wish for the script ...

Retrieve the variable declared within the event

How can I access a variable set in an event? Here is the code snippet: $scope.$on('event_detail', function (event, args) { $scope.id = args; console.log($scope.id); // This prints the correct value }); console.log($scope.id); // ...

Resolving the issue of data transmission within Yii2's framework: Page-to-page data

I'm currently using Yii2-advanced-app and I have encountered an issue. I want to pass the selected value from a dropdown menu to a PHP code that displays a list with checkboxes on the same page. Here is an image for reference on what I am trying to ac ...

Change the border color of a form field in a material design if the user interacts with the

Is there a way to change the border color of a material form field in Angular when it is touched, focused, or active? I attempted to modify the color by overriding material css-class and also tried creating my own css class, but neither method had any ef ...

Why does "height: auto;" not function properly when I implement "float:left"?

I have set up three responsive columns and now I want to add a wrapping div for them. Although I have created the wrap div, it seems to only work with pixel values. I would prefer to use percentages or auto as I am aiming for a responsive layout. For exa ...

Adaptable Collection of 4 Images

I am facing a challenge in replicating eBay's 'Today' featured seller layout with 4 square images creating one box (refer to the image). I am using Bootstrap for this task, and I am finding it difficult to comprehend how to achieve this. I h ...

Access the freshly launched URL

I am relatively new to PHP and have a table displayed on my webpage. Each row in the table has an auto-incremented rowid that is not being displayed. When clicking on a row, it opens a new page in the format of www.newpage.com/newpage/rowid. If I inspect ...

Error encountered when connecting to MongoDB: 'MongoServerSelectionError'

const uri = 'mongodb+srv:<username>//:<passwod>@chatapp-qrps3.azure.mongodb.net/test?retryWrites=true&w=majority' const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }) client.c ...

"Enhancing event handling: Using addEventListener natively with selectors similar to .on()

Has anyone figured out how to replicate jQuery's .on() method in vanilla JavaScript? The native addEventListener function doesn't seem to have the capability to filter based on child/selector elements, and delving into event bubbling and capturin ...

What is the meaning of '=>' in typescript/javascript?

I keep coming across lots of '=>' in the code I found on the internet. Could someone please explain it to me as if I were 5 years old? (I'm searching for the specific code, and I'll share it here once I locate it).. Found it: ...

Fluid div causing navigation to display improperly

I am currently working on a navigation design where I want to have an image above each list item instead of having individual images for each item. It looks great when the screen is at full size, but as soon as I minimize it, the list items start stacking ...

How to successfully load the google-map-react library

After installing the google-map-react library locally in my app, I verified that it is listed in my package.json under dependencies. The corresponding folder also exists in the node_modules directory. However, when attempting to reference the component con ...

Tips for ensuring all data is downloaded in Firebase (Firestore) Storage before proceeding?

I am currently developing a light dashboard in Vue that connects to Firestore and Storage. As someone who is not an expert, I have encountered a roadblock in what should be a simple task. The issue lies with a function that is meant to retrieve all URLs ba ...

Retrieve the value of a Material UI Slider during the onDragStop event

As I work with a Material UI Slider in my React app, I am aiming to trigger the event onDragStop instead of onChange</code to reduce the number of times the event is fired. However, according to the <a href="https://mui.com/material-ui/react-slider" ...

Using jQuery and JavaScript: The recursive setTimeout function I created accelerates when the tab is no longer active

I am facing a unique challenge with my jQuery slideshow plugin that I'm currently developing. Although the code is running smoothly, I have observed an issue where if I leave the site open in a tab and browse elsewhere, upon returning to the site (us ...

Rendering of @font-face on Windows 7 was executed flawlessly

I have been utilizing @font-face to implement custom fonts on a website. The custom font displays correctly in Firefox, IE, Safari, and Chrome on Mac. However, when viewed on Chrome in Windows 7, the text appears green at 10px instead of black or grey. ... ...

Error with Bootstrap 4 tabs and JavaScript AJAX implementation

I have implemented Bootstrap 4 tabs to showcase content fetched through an AJAX call. However, I encountered an issue upon completion of the call. The error message displayed is: Uncaught TypeError: $(...).tab is not a function The tabs are initially hi ...

Center text vertically even when its number of lines is unknown

Can anyone help me figure out how to vertically center text that can be one or two lines long? The text also has a specific line-height set. I attempted the solution provided in this link but it didn't produce the desired result: <div> < ...

What is the most efficient method for storing text in my web application?

Hey there, I'm looking for a way to easily store and access the wording used in my application so that I can use them again whenever needed. For example, loading messages, validation messages, or informational messages are some of the wordings I want ...

Angular: Incorporating a custom validation function into the controller - Techniques for accessing the 'this' keyword

I'm currently working on implementing a custom validator for a form in Angular. I've encountered an issue where I am unable to access the controller's this within the validator function. This is the validator function that's causing tr ...