How do you add data to an option tag?

Kenneth Rhodes

I'd like to add a data value to an option tag in a select element. For example data-price but my problem is that I have multiple select tags. So how do I get JavaScript to grab the data value of that option that the user selects?

How I want it to work:
Select element #1 contains:

<select onchange="updatePrice()">
<option data-price="1">Apple $1</option>
<option data-price="2">Potato $2</option>
<option data-price="3">Bag of Grapes $3</option>
</select>

Select element #2 contains:

<select onchange="updatePrice()">
<option data-price="5">Really good cake</option>
<option data-price="15">Super Good Cake</option>
</select>

Then I'm honestly not sure what to do in the JS... But I want it to grab what the user selected, get the data-price then calculate the total (just by adding select1 + select2).

EDIT: My answer is different than this question because my question is more specific and requires different methods. Even though this is specific it could help a developer in the future by the answers it gets. Therefore it is not a duplicate but a more specific question than the other. Though that question has a simpler answer that could be plugged in if the developer knows how to.

wisn

This is a bit tricky. You need to give an identifier so our code won't get confused.

<select id="goods1" onchange="updatePrice(this)">
    <option data-price="0">Select one</option>
    <option data-price="1">Apple $1</option>
    <option data-price="2">Potato $2</option>
    <option data-price="3">Bag of Grapes $3</option>
</select>

<select id="goods2" onchange="updatePrice(this)">
    <option data-price="0">Select one</option>
    <option data-price="5">Really good cake</option>
    <option data-price="15">Super Good Cake</option>
</select>

First, add a global variable to storing current price. Second, store the identifier and the price value. Finally, manipulate the current price.

<script>
  let price = 0;
  let stored = {};

  const updatePrice = elm => {
    const id = elm.id;
    const selectedPrice = parseInt(
      Array.from(
        elm.children
      ).filter(x => x.selected)[0].dataset.price
    );

    price = 0;
    stored[id] = selectedPrice;

    Object.keys(stored).forEach(key => price += stored[key]);

    console.log(`Price: ${price}`);
  };
</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add JSON data in select and option tag?

How do you add an `li` tag only data exists in an array in html/css/javascript?

How do you add an option to CMake called by python

Java: How do you add data into an object?

How do you add text in a .data()?

How do you add an icon to a button_tag using HAML?

how do you add text to a tag in js using innertext and add to an id of a div?

How to add <a> tag in dropdown option value

How do you add data bars to a chart via Java?

how to add new option for select tag in select2 - remote data

How do you remove all the options of a select box and then add one option and select it with jQuery?

how do you add attributes to script tag in jade e.g. async defer

how do you add a poster image in an <iframe></iframe> tag before the start of a video?

How do you undo setting an option in vim?

How do you revert to a specific tag in Git?

How often do you tag in Git?

How do you change length of an hr tag?

How do you center an anchor tag?

How do you select an option from a multiple option interatcive installer?

How do you set a HTML data- tag for a ReactJS TextField in a form

How do you get my rails radio_button_tag to save the data on form submission?

How to dynamically add items to select tag option (dropdown) in React

How to add values to <option> tag loaded from array

How can I add option values in the select tag with PHP

How to add search box inside <select> tag with ng-option

How do I add a debug option to Makefile

How do I add a Lead Source option

How do I add option 2?

How to populate the txt data into the select tag as both option value and within option tag