JavaScript How to calculate the total number of passengers calculate total number of combinations,calculate total number of days,

I want to code to calculate the total number of adult, child and baby passengers. I tried with jquery but did not count the total number of all the passengers.

Thanks in advance

https://jsfiddle.net/devefrontend/21036h5c/12/

$("select").on('change', function() {
    var str = "";
    $(".passenger option:selected").each(function() {
        str += $(this).text() + " ";
    });
     $("#count").text(str);
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link target='_blank' href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="form-group">
    <label>Dewasa <span id="count">0</span></label>
    <select name="adult" class="form-control passenger">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
        <option>6</option>
    </select>
    <select name="child" class="form-control passenger">
        <option>0</option>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
        <option>6</option>
    </select>
    <select name="infant" class="form-control passenger">
        <option>0</option>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
        <option>6</option>
    </select>
</div>
Answer:1

You were trying to add strings as numbers. The following code takes the textual values of the options and converts them to numbers.

$("select").on('change', function() {
        var count = 0;
        $(".passenger option:selected").each(function() {
            count += parseInt($(this).text());
        });
        $("#count").text(count);
    }).change();
label {
  display:block;
  background:skyblue;
  color:#fff;
  padding:20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
                  <label>Dewasa <span id="count">0</span></label>
                  <select name="adult" class="form-control passenger">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                  </select>
                  <select name="child" class="form-control passenger">
                    <option>0</option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                  </select>
                  <select name="infant" class="form-control passenger">
                    <option>0</option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                  </select>
                </div>
Answer:2

When working with values, make sure to use parseInt(), otherwise Javascript might see it as a string and act accordingly.

Answer:3

You should convert the text of the selected option to an int value, and to do that use parseInt() function:

$("select").on('change', function() {
        var str = 0;
        $(".passenger option:selected").each(function() {
            str += parseInt($(this).text()) ;
        });
        $("#count").text(str);
    }).change();
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link target='_blank' href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="form-group">
                  <label>Dewasa <span id="count">0</span></label>
                  <select name="adult" class="form-control passenger">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                  </select>
                  <select name="child" class="form-control passenger">
                    <option>0</option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                  </select>
                  <select name="infant" class="form-control passenger">
                    <option>0</option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                  </select>
                </div>
Answer:4

You should convert the values to integer if you want to get the total. Update your Code as shown below.

$("select").on('change', function() {
    var total = 0;
    $(".passenger option:selected").each(function() {
        total += parseInt($(this).val());
    });
    $("#count").text(total);
}).change();
Answer:5

I have an array as such: [[1,3],[2,5],[1,3],[2,5]] and i would like to remove any duplicate sub-arrays. I tried using this code: uniqueArray = array.filter(function(item, pos) { return array....

I have an array as such: [[1,3],[2,5],[1,3],[2,5]] and i would like to remove any duplicate sub-arrays. I tried using this code: uniqueArray = array.filter(function(item, pos) { return array....

  1. remove duplicate arrays javascript
  2. remove duplicate arrays
  3. remove duplicate arrays python
  4. remove duplicate arrays php
  5. removing duplicate array in java
  6. removing duplicate array elements
  7. remove duplicate array js
  8. remove duplicate array object javascript
  9. remove duplicate array elements javascript
  10. remove duplicate array values php
  11. remove duplicate array swift
  12. remove duplicate array in c
  13. remove duplicate array values javascript
  14. remove duplicate array vba
  15. remove duplicate array typescript
  16. remove duplicate array jquery
  17. remove duplicate array elements in java
  18. remove duplicate array lodash
  19. remove duplicate array ruby
  20. remove duplicate array javascript es6

I have following code in my javascript var _csrf = $("#csrf_token").val(); var xhr = $.ajax ({ url : 'http://ip.jsontest.com/', type : 'GET', contentType ...

I have following code in my javascript var _csrf = $("#csrf_token").val(); var xhr = $.ajax ({ url : 'http://ip.jsontest.com/', type : 'GET', contentType ...

  1. error ajax call
  2. error ajax call jquery
  3. ajax error call function
  4. error ajax request failed
  5. error ajax request
  6. error ajax request failed joomla
  7. cors error ajax call
  8. error during ajax call
  9. error 500 ajax call
  10. 400 error ajax call
  11. get error ajax call
  12. error message ajax call
  13. 415 error ajax call
  14. success error ajax call
  15. error handling ajax call
  16. 404 error ajax call
  17. catch error ajax call
  18. return error ajax call
  19. ajax error request entity too large
  20. ajax error request object

I'm trying to run this jestJS unit-test, but I do get TypeError: Promise resolver undefined is not a function. What am I doing wrong? it('_onSubmit() should throw error if data is missing', (done) =&...

I'm trying to run this jestJS unit-test, but I do get TypeError: Promise resolver undefined is not a function. What am I doing wrong? it('_onSubmit() should throw error if data is missing', (done) =&...

  1. promise resolver is not a function

I need to use a google projects service account to access google API using JavaScript. In order to do this I need to OAuth2 to google API servers to get an auth token. I understand that Google ...

I need to use a google projects service account to access google API using JavaScript. In order to do this I need to OAuth2 to google API servers to get an auth token. I understand that Google ...

  1. retrieve service account password
  2. cannot retrieve service account
  3. retrieve windows service account password
  4. cannot retrieve service account openshift
  5. retrieve managed service account password
  6. openshift build cannot retrieve service account