JavaScript Calculate Values OnSelect & Update

I have a dropdown selection field and other fields. It goes like this,

<div class="form-group"><label class="sr-only" for="id_max_size">Max 
   Size</label><select name="max_size" title="" required class="form-control" id="id_max_size">
       <option value="" disabled selected>Size ↓</option>
       <option value="10">AAA</option>
       <option value="20">BBB</option>
       <option value="30">CCC</option>
       <option value="40">DDD</option>
    </select>
  </div>

 <input type="number" name="no_needed" title="" required id="id_no_needed" step="0.01" />
 <input type="number" name="total_price" title="" required id="id_total_price" step="0.01" />

Let's say a user selects AAA, I want to get the value of 'AAA' which is 10 and multiply by 'no_needed' field value inserted by the user and make the result insert in total_price field. Even if the user selects CCC it should follow the same process.

$(document).ready(function () {
   $('select').on('change', function() {
   $('.sr-only').text($('select[name=max_size]').val() * 
   ($('select[name=no_needed]').val()));
  });

 });

It's not working. I really don't know javascript. How do I accomplish this?

Answer:1

You have a few mistakes:

  • You are using select to select an input--use $('input') instead.
  • You can use this to refer to the relevant select element inside the event listener callback.

Try this:

$(document).ready(function () {
   $('select').on('change', function() {
   $('input[name=total_price]').text($(this).val() * 
   ($('input[name=no_needed]').val()));
  });
});
Answer:2

Does anyone know how to do a pull to refresh in a mobile site? I can see some conflicting issues that could be found from trying to implement this such as Chrome already does this from the browser by ...

Does anyone know how to do a pull to refresh in a mobile site? I can see some conflicting issues that could be found from trying to implement this such as Chrome already does this from the browser by ...

  1. angularjs pull to refresh

I want to reduce space between label and text box, you can see in bellow image, I used Ext.form.field.Text control of ext JS. Please see bellow code for this control. this.temp1min = new Ext.form....

I want to reduce space between label and text box, you can see in bellow image, I used Ext.form.field.Text control of ext JS. Please see bellow code for this control. this.temp1min = new Ext.form....

  1. reduce space between bullet and text
  2. reduce space between lines word
  3. reduce space between bullet and text powerpoint
  4. reduce space between bars excel
  5. reduce space between sections latex
  6. reduce space between subplots matplotlib
  7. reduce space between letters in word
  8. reduce space between items latex
  9. reduce space between subplots matlab
  10. reduce space between paragraphs html
  11. reduce space between equations latex
  12. reduce space between lines css
  13. reduce space between columns word
  14. reduce space between figure and caption latex
  15. reduce space between lines latex
  16. reduce space between paragraphs latex
  17. reduce space between blocks wordpress
  18. reduce space between title and text latex
  19. reduce space between lines html
  20. reduce space between columns latex

I am using the Chart.js library to display some values in stacked bars but I am struggling in trying to find out how to display the values inside of the bars, that is, Right now, I have the following ...

I am using the Chart.js library to display some values in stacked bars but I am struggling in trying to find out how to display the values inside of the bars, that is, Right now, I have the following ...

  1. display variable values inline while debugging

I'm trying to override the default TinyMce 4 spellchecker method "markErrors", but without success. I've already tried: // on editor init... editor.plugins.spellchecker.markErrors = function() { ...

I'm trying to override the default TinyMce 4 spellchecker method "markErrors", but without success. I've already tried: // on editor init... editor.plugins.spellchecker.markErrors = function() { ...