Struggling to combine select dropdown choices in one calculation

​​I'm currently working on a project where I need to create a website that multiplies three numbers selected from dropdown menus together. However, when I attempt to perform the calculation, I only get the result "1" displayed. I've spent several hours searching for solutions but haven't been able to find any changes that resolve the issue. Any help or suggestions would be greatly appreciated. Thank you!

<!doctype html>

<html lang="en>
<head> 
<title>
Test Project
</title>
<link rel="stylesheet" href="main.css" />

</head>
<meta charset="utf-8" /> </meta>
<body>

<div id="div_1" align="center">

    <header id="top_header">
        <h1>Test Project</h1> 
    </header>


</div>

<div align="center">

<select id="Section1">
    <option value="1"> 1</option>
    <option value="2"> 2</option>
    <option value="3"> 3</option>
    <option value="4"> 4</option>
    <option value="5"> 5</option>
    <option value="6"> 6</option>
    <option value="7"> 7</option>
    <option value="8"> 8</option>
    <option value="9"> 9</option>
    <option value="10"> 10</option>
</select>

<select id="Section2">
    <option value="1"> 1</option>
    <option value="2"> 2</option>
    <option value="3"> 3</option>
    <option value="4"> 4</option>
    <option value="5"> 5</option>
    <option value="6"> 6</option>
    <option value="7"> 7</option>
    <option value="8"> 8</option>
    <option value="9"> 9</option>
    <option value="10"> 10</option>
</select>

<select id="Section3">
    <option value="1"> 1</option>
    <option value="2"> 2</option>
    <option value="3"> 3</option>
    <option value="4"> 4</option>
    <option value="5"> 5</option>
    <option value="6"> 6</option>
    <option value="7"> 7</option>
    <option value="8"> 8</option>
    <option value="9"> 9</option>
    <option value="10"> 10</option>
</select>

    <script>
function myFunction() {
var x = document.getElementById("Section1").selectedIndex;
var y = document.getElementById("Section2").selectedIndex;
var z = document.getElementById("Section3").selectedIndex;
   alert(document.getElementsByTagName("Option")[x * y * z].value);
 }
</script>

<button onclick="myFunction()">Calculate</button>

</body>
</html>

Answer №1

The code snippet [x * y * z].value doesn't seem to be logical. Consider the following alternative:

function calculateValues() {
    var x = document.getElementById("Section1").value;
    var y = document.getElementById("Section2").value;
    var z = document.getElementById("Section3").value;
    alert(x * y * z);
}

You can also opt for using the selectedIndex method:

var section1 = document.getElementById("Section1");
var x = section1.options[section1.selectedIndex].value;
// continue with other sections accordingly..

Although, this approach may not be as straightforward.

Answer №2

This code snippet

alert(document.getElementsByTagName("Option")[x * y * z]);
will retrieve an option element with the index of x*y*z from the collection of all option elements on the current site.

Are you looking to achieve this specific result? Or are you simply interested in multiplying x, y, and z together:

alert(x * y * z)

If you want to see it in action, check out this jsfiddle link for a demonstration: http://jsfiddle.net/k475jso9/

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

Creating a flexible Tic Tac Toe board using only HTML and CSS without any JavaScript

I'm currently tackling an HTML layout challenge that involves creating a responsive Tic Tac Toe board using only HTML and CSS, without any JavaScript. This is how I have structured the layout of the board: <div class="board"> <div class= ...

When resizing the window, navigation items vanish

I've implemented the collapse feature using Bootstrap, but encountered an issue where my nav elements disappear when resizing the window. Do you have any suggestions on how to fix this? Here's a snippet of my code: Navb ...

I would like to inquire about the process of accessing profile information on a website through the LinkedIn API

Do you know how I can use the latest LinkedIn JavaScript API with OAuth 2.0 to retrieve my own profile details for a website? The goal is to automatically update the website using my linked profile information. I have attempted the following: api_key: ...

Attempting to update the state by utilizing the values provided by useContext

Hey there! I'm currently working on a Gatsby React app and my main objective on this particular page is to remove some localStorage and reset context AFTER rendering. The timing of this reset is crucial because I need the page to initially render with ...

Place each label and input element on a separate line without using div tags

Can we separate form elements onto individual lines without enclosing them within divs? For example: <label for="one">One:</label> <input type="text" id="one"> <label for="two">Two:</label> <select id="two"> ...

Retrieving information from a virtual document in a 'pre' save hook using Mongoose

Seeking help with utilizing data from a recently created document to update a value using a 'pre' hook. An example of the model being used: ... title: { type: String, required: true }, company: { type: mongoose.Schema.ObjectId, ref: &ap ...

Should I reload the entire table or insert a row manually?

This unique ajax question arises: within a table lies the users' information, displayed based on individual settings and timing. Sometimes, users instantly see the data, other times they must wait for it - their choice determines when it appears. Wha ...

What is the best way to break down this function from props in React?

Forgive me if this question sounds naive, but as I delve into the world of React and useState, I am encountering a scenario where I have a signup function coded. Upon sending a username and password through a POST request to an API endpoint, a response mes ...

React-Native Error: Invalid element type detected

While attempting to run my React Native app on my iPhone using Expo, I encountered an error displayed in a red background area. Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite ...

How can I position a div in the center of a fullpage.js slide?

I'm currently utilizing the fullPage.js plugin sourced from this link: . I have successfully implemented vertical slides by using the following HTML code: <div class="section" id="section2"> <div class="slide" id="slide1"> ...

I am looking to integrate a WordPress blog post into my Bootstrap website

Is there a way to showcase the 3 most recent posts from my WordPress blog (www.xyz.wordpress.com) on my Bootstrap HTML website without using PHP? Feedburner got me close, but I'm limited in CSS styles and don't want the "Headline by Feedburner" ...

The React JS @material-ui/core Select component encountered an error when attempting to access a property that does not exist: 'offsetWidth'

Struggling with the Select component from @material-ui/core, encountering the error below: Cannot read property 'offsetWidth' of null Any assistance would be greatly appreciated. Link: codesandbox Code: import React from "react"; import { ...

Exploring the contrast of && and ?? in JavaScript

My current focus is on utilizing the Logical AND && and Nullish coalescing operator ?? in handling conditional rendering of variables and values. However, I find myself struggling to fully comprehend how these operators function. I am seeking clar ...

Implementing restriction measures for deterring harmful conduct within ExpressJS

Recently, I was alerted to some potential vulnerabilities in the application I'm currently developing, particularly in relation to the JavaScript code on the front-end. These flaws could allow a user to exploit the system by clicking multiple buttons ...

Eliminate a specific element from the dataset

My question revolves around a data array used to populate three drop down <select> boxes. How can I eliminate duplicate values within the drop downs without affecting the array itself? For example: data = { firstbox_a: ['grpA_1','gr ...

What could be causing the failure to retrieve the salt and hash values from the database in NodeJS?

My current issue involves the retrieval of hash and salt values from the database. Although these values are being stored during sign up, they are not being retrieved when needed by the application. Below, you will find snapshots of the database, console s ...

Electron fails to display images in the compiled version

I'm currently troubleshooting an issue related to displaying images using CSS in my electron project. In the latest version of the application, the images are not showing up when linked from a CSS file. However, in a previous version, the images disp ...

Ensuring User Data is Current in the UI with Firebase Auth Callbacks

Below is the standard method for setting the user state to currentuser that is returned from onAuthStateChanged. I am looking for a useEffect hook that will be triggered whenever there is an update to the user's information. Unfortunately, I am unable ...

What is the best way to incorporate a unique box shadow onto my svg image?

While I've seen a similar question here: How to add box-shadow to a svg path circle shape, I am struggling to grasp how the transition from box shadow characteristics to SVG drop shadow characteristics was achieved. (Especially given that SVG Drop sha ...

I require assistance in displaying a dynamic, multi-level nested object in HTML using AngularJS

I have a complex dynamic object and I need to extract all keys in a nested ul li format using AngularJS. The object looks like this: [{"key":"campaign_1","values":[{"key":"Furniture","values":[{"key":"Gene Hale","values":[{}],"rowLevel":2},{"key":"Ruben A ...