Using JavaScript to print radio type buttons

Currently working on a web page and I've encountered a problem that's got me stumped. There are two sets of radio buttons - the first set for package dimensions and the second set for weight. The values from these buttons are assigned to variables PV_it and PV_eu in a JavaScript function. The task at hand is to determine which variable (PV_it or weight) has the higher value and display it within a specific div element. Any thoughts or suggestions on how to approach this would be greatly appreciated as I'm currently stuck and unsure of where to start.

Answer №1

According to the input from other users, it is important for radios that are grouped together to have the same name. This allows you to easily access the selected value in your script.

Feel free to explore this reactive Vue example I've put together for you.

new Vue({
  el: '#app',
  data() {
    return {
      dimensions: [
        "31 x 16 x 10", "33 x 20 x 15", "35 x 25 x 17", "40 x 31 x 20", "40 x 35 x 25", "41 x 40 x 30", "45 x 45 x 37", "50 x 50 x 40", "60 x 50 x 50"
      ],
      weights: [
        "0 - 1", "1 - 2", "2 - 3", "3 - 5", "5 - 7", "7 - 10", "10 - 15", "15 - 20", "20 - 30"
      ],
      selectedDimension: -1,
      selectedWeight: -1,
      prices: [
        0.83, 1.65, 2.48, 4.13, 5.83, 8.2, 12.49, 16.67, 25
      ]
    }
  },
  computed: {
    highestSelection() {
      return this.selectedDimension > this.selectedWeight? this.selectedDimension : this.selectedWeight;
    },
    managementFee() {
      return 0; // add your calculation here
    },
    returnRate() {
      return 0; // add your calculation here
    },
    storageFee() {
      return 0; // add your calculation here
    },
    shipmentItaly() {
      return this.highestSelection != -1 ? this.prices[this.highestSelection]: 0; // example only. add your calculation here
    },
    shipmentEurope() {
      return this.shipmentItaly * 1.2; // example only. add your calculation here
    },
    shipmentWorldwide() {
      return this.shipmentItaly * 2; // example only. add your calculation here
    }
  },
  filters: {
    price(val) {
      return val.toFixed(2);
    }
  }
})
@import url('https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap');

#app {
  font-family: 'Roboto Mono', monospace;
  display: grid;
  gap: 1rem;
  justify-content: center;
}

#app > * {
  padding: 1rem;
  background: #eee;
}

.dimensions, .weights {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  grid-auto-flow: column;
  gap: .5rem;
}

.prices {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.min.js"></script>
<div id="app">
  <div class="dimensions"><label v-for="(dimension, i) in dimensions" :key="`dimension-${i}`"><input type="radio" name="dimension" v-model="selectedDimension" :value="i">{{ dimension }} cm</label></div>
  <div class="weights"><label v-for="(weight, i) in weights"  :key="`weight-${i}`"><input type="radio" name="weight" v-model="selectedWeight" :value="i">{{ weight }} kg</label></div>
  <div class="prices">
    <div>MANAGEMENT FEE</div>
    <div>RETURN RATE</div>
    <div>STORAGE FEE</div>
    <div>{{ managementFee | price }} €</div>
    <div>{{ returnRate | price }} €</div>
    <div>{{ storageFee | price }} €</div>
  </div>
  <div class="shipments">
    <div>{{ shipmentItaly | price }} € Italy Shipping</div>
    <div>{{ shipmentEurope | price }} € Europe Shipping</div>
    <div>{{ shipmentWorldwide | price }} € WORLDWIDE* ZONE 6</div>
  </div>
</div>

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

"Vue Filepond: Enhancing Your Images with Cropping

Currently, I am integrating filepond with VueJS to facilitate image uploads. To enable image cropping during upload, a specific configuration is required. The filepond plugin has been globally registered, as shown below: import Vue from 'vue'; i ...

Looking to automate the scraping of Wikipedia info boxes and displaying the data using Python for any Wikipedia page?

My current project involves automating the extraction and printing of infobox data from Wikipedia pages. For example, I am currently working on scraping the Star Trek Wikipedia page (https://en.wikipedia.org/wiki/Star_Trek) to extract the infobox section d ...

React, facing the challenge of preserving stored data in localStorage while accounting for the impact of useEffect upon

Seeking some assistance with the useEffect() hook in my Taking Notes App. I've set up two separate useEffects to ensure data persistence: one for when the page first loads or refreshes, and another for when a new note is added. I've added some l ...

Utilizing Google Tag Manager for Efficiently Managing Multiple Schema Product Reviews with JSON-LD and Variables

Currently, I am facing a challenge while using Google Tag Manager to incorporate Schema JSON-LD Product reviews on certain pages. Despite my efforts, I am unable to locate any relevant resources to resolve this issue. The main problem lies in informing GT ...

TextBox with MultipleLines

I'm looking to add functionality to a text area with formatting capabilities as shown in the diagram. Can anyone guide me in the right direction? Also, whenever I click on the text area, a formatting tool should automatically appear above it. This i ...

Latest News: The store is now received in the mutation, instead of the state

An update has been made to this post, please refer to the first answer After thorough research, I couldn't find a solution to my issue despite checking several threads. This is my first time using the Quasar framework and it seems like I might have o ...

Need help with resetting a value in an array when a button is clicked?

Using Tabulator to create a table, where clicking on a cell pushes the cell values to an array with initial value of '0'. The goal is to add a reset button that sets the values back to '0' when clicked. component.ts names = [{name: f ...

Import the JSON data into the designated storage unit

$(".btn-primary").click(function(){ runSuccessFunction(jsonObj); }); If I need to update the JSON data in the JSFiddle Code example, how can I reload only the container by clicking a button? I want to run the code every time I make a change to the J ...

Unusual occurrences within stacked MUI Popper components

Below is a sample Node component that uses a button element to anchor an MUI Popper: type Props = { children?: React.ReactNode; }; const Node = ({ children }: Props) => { const [anchor, setAnchor] = React.useState(null); return ( <div> ...

I am looking for a way to have one element as a child of another element without automatically adopting specific properties

https://i.sstatic.net/iuNB7.png <span id="priority-dot-open-menu"> <span id="priority-menu"> <span class="tooltip-top"></span> <span id="priority-dot-blue">& ...

javascript issue with attribute manipulation

My current struggle involves setting the attribute of an element through programming, but I keep encountering an error in Firebug: obj.setAttribute is not a function. Since I am working with jQuery, allow me to provide some additional code for better conte ...

Ensure that a div element expands to 100% of its parent's height when its child's height is less than 100%

I am struggling with a flex div that contains 2 elements. I want both elements to take up the full parent height while their content remains at auto height, but I keep getting the inner content height. Here is my code: <html lang="en"> <head&g ...

Is it possible to use vanilla JavaScript scroll event with AngularJS framework?

I am attempting to track the window offset from the top of the document, but I am facing issues with jQuery scroll functionality. Can a vanilla JavaScript scroll event listener be used effectively within an Angular environment? app.directive('owlCaro ...

Trigger an event, pause, and subsequently trigger another one within Vue

I have successfully emitted the following events to the parent component: this.$emit('sendToParent1', true); this.$emit('sendToParent2'); this.$emit('sendToParent3'); this.$emit('sendToParent4', true); this.$emit(&ap ...

Implementing a redirection of Dojo Data grid cell values to a different destination

I am working with a Dojo data grid that contains 10 rows (cells) of values. When the first row is clicked, I need it to redirect to another HTML page and carry along the values from the first row. Can you assist me in achieving this functionality? ...

Tips for retrieving the value of a corresponding data attribute by the text within a div using jQuery in JavaScript

I have several divs with different names and values assigned to the data attribute data-category Now, I am in need of retrieving the value of data-category based on specific text present inside the div. <div class='ext-category' data-categor ...

Uncertain about troubleshooting the `uid: prismicDocument.uid ?? void 0` error on a Prismic and Next.js website?

Currently, I am working on a Next.js project integrated with the Prismic CMS. The website runs smoothly in my local environment, however, after some recent updates to the content, I encountered the following error during production builds: 2:42:19 PM: /opt ...

How can I retrieve the class of the parent element by referencing the child id in jQuery?

I want to trigger an alert on click from the child id <th id="first"> to the table(parent) class. alert($(this).parent('tr').attr('class')); This code helped me to get the class of the <tr>. However, when I try to get the ...

The styling of HTML elements is ineffective when using scoped styles

I'm facing an issue where my element styling doesn't seem to work when using the scoped attribute. Upon inspecting the element, it appears that the styling is not being applied when using scoped. The reason I need to use scoped is because I want ...

Using jQuery to alter hover color using a dynamic color picker

I'm looking to update the hover color using a color picker tool. Here are the methods I've attempted: // Initial Attempt $("input[type=color]").change(function(e) { var selectedColor = e.target.value; // $("body").css("background-color ...