JavaScript Javascript select, get value from name javascript select value from object,javascript select value from dropdown,

I'm building dynamic drop downs for an order form for additional options. When selected these options increase the value for a dynamic amount. The drop downs are organized as such:

<option value=" . $name . ">" . $name . "</option>

This is via php and then I just echo it. What I don't know how to do is currently my javascript is set to determine how much to increment the value by the option value, which leaves me looking at numbers in the database and not really knowing what they ordered. So what I thought to do is instead have the php echo something like: <option value="Test"> Test - $10 </option> where I could use javascript to cut out the 10 and add it to the total. This is for multiple dropdowns, currently my javascript is:

function getDDValue(elementID) {
    var element = document.getElementById(elementID);
    var value;
    // if the element exists
    if (element) {
        value = element.options[element.selectedIndex].value;
    } else {
        value = 0;
    }
    console.log(value);
    return parseFloat(value);
}

function getBase() {
    var value = 0;
    var element = document.getElementById('base_cost');
    value = element.value;
    return parseFloat(value);
}

function getTotal() {
    var optA = getDDValue("optA");
    var quantity = getDDValue("quantity");
    var optB = getDDValue("optB");
    var optC = getDDValue("optC");
    var base_cost = getBase("base_cost")
    var totalPrice = '0';
    totalPrice = parseFloat((+base_cost + +optA + +optB + +optC) * +quantity);
    console.log(totalPrice);
    document.getElementById('totalPrice').innerHTML = "<?php echo $name; ?> $" + totalPrice;
}
getTotal();

I'd post what I've tried but I'm not sure where to even start, and as sure as I am that this has been done before I must be asking google and SO the wrong questions in my searches so I haven't found any resources on this. Any help is greatly appreciated on the best way to achieve this. Thanks!

Answer:1

You aren't using field correctly, the best way to use a input is:

<select name="Test" id="Test" onChange="getTotal(); or any function">
 <option value="10">Test - 10</option>
</select>

For your current need:

If you're using JQuery, you can spit the Text with "-", and filter second part like $(secondPart).replace(/[^\d.]/g, ""); to get only a valid number.

Answer:2

Say i have conditions setup like this: if (condition1) { for (var i in objx) { if (objx[i] == 3) { //go to next else if } } } else if(condition2) { ... } so there ...

Say i have conditions setup like this: if (condition1) { for (var i in objx) { if (objx[i] == 3) { //go to next else if } } } else if(condition2) { ... } so there ...

I have a datalist that looks like : <datalist id="foodlist"> <option value="one" ></option <option value="two" ></option> <option value="three" ></...

I have a datalist that looks like : <datalist id="foodlist"> <option value="one" ></option <option value="two" ></option> <option value="three" ></...

I am trying to implement infinite scroll on my website and it works well on browsers on any computer, but it has issues on mobile browsers. So far, on chrome, if I don't zoom in or out it works ok ...

I am trying to implement infinite scroll on my website and it works well on browsers on any computer, but it has issues on mobile browsers. So far, on chrome, if I don't zoom in or out it works ok ...

  1. infinite scroll mobile
  2. infinite scroll jquery mobile
  3. vue infinite scroll mobile
  4. infinite scroll not working on mobile
  5. infinite scroll chrome mobile
  6. infinite scroll angularjs mobile

I am working on a angularjs app. I am receiving the data as an array from back-end. I need to convert that in to a html list. (using li) but how can i do that? here is the array string i am getting :...

I am working on a angularjs app. I am receiving the data as an array from back-end. I need to convert that in to a html list. (using li) but how can i do that? here is the array string i am getting :...

  1. convert string to valid json
  2. convert string to valid datetime in c#
  3. convert string to valid json c#
  4. convert string to valid filename python
  5. convert string to valid json python
  6. convert string to valid url javascript
  7. convert string to valid url c#
  8. convert string to valid filename
  9. convert string to valid filename c#
  10. convert string to valid html id
  11. convert string to valid url
  12. convert string to valid date in r
  13. convert string to valid json online
  14. convert string to valid xml
  15. convert string to valid xml c#
  16. convert string into valid date in r
  17. convert string to valid xml java
  18. convert string to valid id
  19. python convert string to valid variable name
  20. c# convert string to valid path