What is the best way to utilize class labels?

I've encountered an issue I need help with. There are two lists of options in play here.

One list has names all starting with 'M', and the other with 'R'. The task is to select specific options, like 'A' and 'C', from both lists. For the selected options, an 'X' bonus should be added for 'X' options, and a 'Y' bonus ('B') for the rest.

The bonus comes in the form of a percentage.

The main challenge is how to accurately select these options and apply the bonuses. Can this be achieved? I've heard that class names may provide a solution, but I'm unsure how to implement them.

            <form action="">
    <fieldset>
<head>
<script type="text/javascript">
function myFunction() {

    /*Left flank bonus*/
    var MLef1 = document.getElementById("MeleeL").value;
    var RLef1 = document.getElementById("RangedL").value;
    var ML = MLef1 - 0;
    var RL = RLef1 - 0;

    
    /*Melee total*/
    var MT1 = ML;
    var MT2 = MT1 / 100;
    var MT = MT2 - 0;
    
    /*Ranged total*/
   var RT1 = RL;
  var RT2 = RT1 / 100;
   var RT = RT2 - 0;

    
    /*Left flank normal*/
/*Left flank melee*/
    var x = document.getElementById("Melee").selectedIndex;
    var y = (document.getElementsByTagName("option")[x].value);
    var xy = document.getElementById("LM1").value;
    

/*Left flank Ranged*/
var p = document.getElementById("Ranged").selectedIndex;
    var o = (document.getElementsByName("LR")[p].value);
    var i = document.getElementById("LM1").value;

/*Ranged*/
    var c1 = o * i;
    var c = c1 - 0;
    var RTZ = RT * c;
    var RTz = RTZ - 0; 
    
    /*Melee*/
    var z2 = y * xy;
    var z = z2 - 0;
    var MTZ = MT * z;
    var MTz = MTZ - 0;


    /*Zero function*/

    if (MT <= 0) {
        (document.getElementById("result").innerHTML = z);
    }
    else if (MT > 0) {
        (document.getElementById("result").innerHTML = MTz);
    
    }
    if (RT <= 0) {
        (document.getElementById("result1").innerHTML = c);
    }
    else if (RT > 0) {
        (document.getElementById("result1").innerHTML = RTz);

   
    
    }
   
    
    
}
</script>

        <legend align="center" id="Defense">Defense</legend>
        
        <table>
            <tr>
                <th>Left Flank</th>
                <th>
                    <th>
                        <th>Center Flank</th>
                        <th>Right Flank</th>
            </tr>
            <tr>
                <td>
                    <label>X Bonus</label>
                    <br>
                    <label>Y bonus</label>
                    <br>
   
                </td>
                <td>
                    <input type="number" id="MeleeL">%
                    <br>
                    <input type="number" id="RangedL">%
                    <br>
                    
            </tr>
        </table>

        <select id="Melee">
            <option value="11">A</option>
            <option value="9">B</option>
            <option value="6">C</option>

        </select>
        <input type="number" style="width:50px" id="LM1">
<select id="Ranged">
    <option name="LR" value="17">A</option>
    <option name="LR" value="4">B</option>
    <option name="LR" value="36">C</option>

        </select><br>

        

        <button type="button" id="buton" onclick="myFunction()">Calculate</button><br>
<p id="result">The Melee, </p><p id="result1">The R, </p>

    </fieldset>
</form>

The bonus amount can vary and will be entered via an input box. Once again, the goal is for users to have the ability to add a variable bonus to some, but not all, options on a given list.

Your assistance is greatly appreciated!

Answer №1

After some trial and error, along with a few hours spent reading coding manuals, I finally figured out how to accomplish this task. I am now utilizing the "data-name1" attribute, which has proven to be very helpful.

    <script>
    document.getElementById("selectElement").selectedIndex = -1; // ensures no option is selected upon page load
    // Great success!!
    
    function getData(){
      var e = document.getElementById("qwert"); // retrieve the <select> element
    // Clear
    
      var data_name1 = e.options[e.selectedIndex].dataset.name1; // fetch the selected //<option> and its name1 data attribute
    
      
    
     // var data_name2 = e.options[e.selectedIndex].dataset.name2;  get the selected //<option> and its name2 data attribute
    
     
    
      var value = e.options[e.selectedIndex].value; // obtain the value of the selected <option>
    
    
    //Outcome
    document.getElementById("data1").innerHTML = data_name1;
      document.getElementById("value").innerHTML = value;  
    }
    </script>
    
    
    
    
    
    <select id="qwert" onclick="getData()">
    <option value="135" data-name1="M">Halberdier</option>
                <option value="150" data-name1="R">Lancer</option>
                <option value="51" data-name1="R">Longbowman</option>
                <option value="27" data-name1="M">Militia</option>
    </select>
    
    <p>
      <label>data-name1 = </label>
      <span>"</span><span id="data1"></span><span>"</span>
    </p>
    
    
    <p>
      <label>value = </label>
      <span>"</span><span id="value"></span><span>"</span>
    </p>

Thank you for all the assistance, my apologies for any confusion. Take care!

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

Maximizing Efficiency with Single Click Line Editing in the Newest Version of jqgrid

There seems to be an issue with single click inline row editing in the latest free jqgrid from github master. BeforeSelectRow, clicking on a row to start inline editing causes an exception: Uncaught TypeError: Cannot read property 'rows' of unde ...

When incorporating babel-plugin-styled-components, Nextjs may encounter a mismatch error with classnames

After transferring the content of _document.js from the following styled components example and implementing the babel plugin mentioned in the styled components documentation, I am still encountering an error. _document.js import Document from 'next/ ...

A guide to removing duplicate values from dropdown menus filled with API data in a React application

I am encountering an issue with my three dropdowns - Manufacturer, Province, and City - as they are fetching data from an API but displaying duplicate values instead of unique ones. Context: The API stores information for approximately 50 products. There ...

Spotting repeated terms within an HTML document

I have a table with results generated using v-html (meaning the text inside the table does not appear until the page is rendered). I want to compare two rows and highlight any duplicate words they may contain. While looking for examples, I came across a p ...

What is the method for arranging objects in AngularJS using a custom sorting sequence?

I would like to display an array of object's properties in a custom sorted order. Here is the initial array: $scope.weekDays = [ { "day" : "TUESDAY", "count": 10 }, { ...

What is the significance of using the variable name "$scope" in coding?

As a newcomer to Javascript, I recently finished reading the book Eloquent Javascript and am now delving into AngularJS from O'Reilly. While working on a code snippet provided in the AngularJS book, I encountered hours of frustration trying to figure ...

Exploring the documentation of node.js with doxygen

When it comes to my C projects, I make sure to document them using Doxygen. Recently, I delved into the world of NodeJs and attempted to document .js files with Doxygen, but unfortunately, no output was generated. Despite my efforts to search for answers ...

Troubleshooting Problem with Creating a Button using HTML Checkbox "onclick" Event

My main goal with the checkbox click event is to achieve the following objectives: 1] Create a button with the id = 'id-of-checkbox'+'some-character-here' in a specific div. 2] Clicking on that button will remove both the button and ...

Discover and modify the values of all the keys within nested JSON arrays and objects using JavaScript

I have a nested JSON data structure consisting of arrays and objects. I am looking to convert all the key values from English to Spanish using JavaScript, NodeJS, or AngularJS. { "firstrootkey" : [ //Array of 6 objects { //1st object ...

What is the best way to incorporate v-divider components in between each v-list-item?

I'm attempting to create multiple v-dividers based on the number of answers I have, so that there is a divider for each answer (4). Following an example from the official documentation, I'm trying something like this but I seem to be stuck. Could ...

What distinguishes the sequence of events when delivering a result versus providing a promise in the .then method?

I've been diving into the world of Promises and I have a question about an example I found on MDN Web Docs which I modified. The original code was a bit surprising, but after some thought, I believe I understood why it behaved that way. The specific ...

The animation feature in AngularJS when integrated with the easy pie chart appears to be malfunctioning

Having trouble getting the animation to work, any suggestions on how to fix it? I attempted to troubleshoot on jsfiddle but couldn't get it to work for some unknown reason. Here is the HTML code snippet: <script> var app = ang ...

Issue with generating PDF in AngularJS: pdfMakeTypeError occurs due to inability to read 'ownerDocument' property within html2canvas

Whenever I try to export by clicking the export button, this code is triggered: $scope.export = function() { html2canvas(document.getElementById('balanceSheet')).then(function(canvas) { document.body.appendChild(canvas); ...

The issue with Ajax's failure to replace periods with commas in numbers remains unresolved

I am facing an issue with my function that retrieves numbers from input fields. The numbers have commas instead of dots for decimals (e.g. 123,5). To perform calculations, I convert them using the replace method and then revert back to commas for displayin ...

What is causing the sorting table to fail in React when using useState?

import React, { useState } from "react"; import "./App.css"; const App = () => { const [data, setData] = useState([ { rank: 1, name: "John", age: 29, job: "Web developer", }, { rank: 2, name: "Micha ...

The connection between Mongoose and the nodejs app cannot be established

I've been attempting to integrate Mongoose with my Node.js environment, but I'm encountering an unexpected error. Previously, I was able to connect with Mongoose using the same commands, but for the past few days, it's been throwing errors. ...

the ReactJS data length is accessible when saving changes, but not when refreshing the browser

I am facing a puzzling issue with my API data in this React component. When I console.log the array of 10 objects from the API, everything seems fine. However, when I try to access "data.data.length" and save the file, it works without any errors displayin ...

When the background image URL is altered, the button size automatically adjusts

Presently, I am working with a button <input id="fireAction" type="button" class="submit" onclick="disableSubmit('preFlight');"><br> Afterwards, I modify the background image of the button under different circumstances For example ...

Restrict the number of rows in a CSS grid

Hello there, I am in the process of creating an image gallery using CSS grid. Specifically, my goal is to initially show just one row of images and then allow users to click a "Show more" button to reveal additional images. You can see my progress here: ...

AngularJS Error: [$injector:nomod] Cannot find module 'module' being loaded

Trying to set up the Karma test runner for my Angular project, but running into a persistent error: Error: [$injector:nomod] Module 'app.auth' is not available! You either misspelled the module name or forgot to load it. If registering a modul ...