1

I have a select dropdown with a number value only but the user sees a descriptor for a service attached to the amount

<option value="400">New Report</option>

So long as the value is a pure #, all the javascript code I use runs all the calcs and totals nicely.

The issue is I can't figure out how to save the number and the descriptor at the same time so the form still calculates and I know the choice the user made.

If I include the text to be parsed with php later on to split the number from the text and save in 2 fields in the DB, my calc result is 'NaN' because text is present in the value field

I have tried to somehow split the number from the text in the onchange event, but that has not worked

eg

<select id="srv1" name="srv1" onChange="calcexp(this.form,str.split('srv1','-','0'));">

the idea is to draw from the value what is on the left of the dash.

as in

 <option value="400-New Report">New Report</option>
2
  • I couldn't get your question. Please simplify. Commented Feb 10, 2019 at 5:41
  • 1
    You should probably read the documentation for split(). Also str doesn't appear to be defined in code shown Commented Feb 10, 2019 at 5:46

3 Answers 3

1

I prefer to create an attribute in all options which has option numeric value. And for calculation, you can pass numeric value by getting attribute. Check below code:

    <select id="srv1" name="srv1" onChange="calcexp(this.form, this.options[this.selectedIndex].getAttribute('data-value'));">
        <option value="400-new" data-value="400">New Report</option>
        <option value="500-old" data-value="500">old  Report</option>
    </select>

Hope it helps you.

Sign up to request clarification or add additional context in comments.

5 Comments

Good practice is to use data- attributes for custom data, not something like attr-value
Thanks @pete I will surely follow this from now.
I tried 'data-value' and 'srv1', but the value is not picked up. Still get NaN. If there was a way to POST the 'data-value- variable, then I could get that to work
Can you show your code to me, so I can check? I have checked my shared code and it is working fine.
I didn't want to fight all the other code that did the calcs so I used this sample and it works now <select id=srv1 name=srv1 onChange="calcexp(this.form,'srv1');"><option value="">Select</option> <option value="400" data-service="New R">New Report</option> </select> <input type=hidden name=service id=service /> <script> $('#srv1').change(function () { var service=$(this).find('option:selected').attr('data-service'); $('#service').val(service); }); </script> then: $service = $_POST['service'];
1

This is how you'd parse a number in a string to the left of a -

let str = "123-etc";
let int = Number.parseInt(str.split("-", 1)[0]);
console.log(int);

Comments

1

You can use data attributes to store the extra text with html elements. You can read about data attributes here. https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.