inaz inaz - 2 years ago 79
jQuery Question

How to change the value of option with jquery

I wrote the below code for a booking flight and it works good.

My only problem is that I can not change the value of each option of each

select
.

For example : I want to set value from
0
to
12
to each
option
. But it only repeated the value of inside option's tags.

How can I set value specially for options not only repeat the text inside them as value?
how can i do this?
here is my snippet:



$(function() {

// Function to create child dropdown
var createChildDropdown = function(i) {

// Create a div in the following format:
// <div class="childs">
// <label for="...">Child (number)</label>
// <select id="..."></select>
// </div>
var $childDropdown = $('<div />', {
'class': 'childs'
});
$childDropdown.append($('<label />', {
'for': 'childDropdown-' + i
}).text('Child ' + i));
$childDropdown.append($('<select />', {
'id': 'childDropdown-' + i
}));

// Define options made available for each child
var options = ['a', 'b', 'c'];
options.forEach(function(option) {
// Create new option element and append to <select>
$childDropdown.find('select').append($('<option />').text(option).attr('value', option));
});

// Return documentFragment so that we can append it
return $childDropdown;
};

// Function to destroy child dropdown
var destroyChildDropdown = function($el, i) {
$el.find('div.childs').get(i).remove();
};

$(".button-click a").on("click", function() {
var button = $(this);
var oldVal = parseInt(button.closest("ul").prev().val());
var newVal = (button.text() == "+") ? oldVal + 1 : (oldVal > 0) ? oldVal - 1 : 0;
var total_value = "";

button.closest("ul").prev().val(newVal);

$(".travel").each(function() {
var cat = $(this).prev('span').text();
total_value += cat + ": " + $(this).val() + ", ";
});

if (oldVal < newVal) {
$('.childDropdowns').append(createChildDropdown(newVal));
} else if (oldVal > newVal) {
destroyChildDropdown($('.childDropdowns'), newVal);
}

total_value = total_value.substring(0, total_value.length - 2);
$(".main").val(total_value);
})
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label>
Count all1 Traveller(s)
<input type="text" name="no_travellers" class="main" value="Adults: 1" placeholder="" />
</label>
<br/>
<label>
<span>Children</span>
<input type="text" class="travel" id="CAT_Custom_410672" name="CAT_Custom_410672" value="0" />
<ul class="button-group button-click">
<li><a href="#" class="small button secondary"><i class="fa fa-plus"><span class="hide">+</span></i></a></li>
<li><a href="#" class="small button secondary"><i class="fa fa-minus"><span class="hide">-</span></i></a></li>
</ul>
</label>
<div class="childDropdowns"></div>




Answer Source

forEach passes the loop index to your callback function as a second parameter - so all you need to do is make your callback take that second parameter, and then use it as the value:

$(function() {

  // Function to create child dropdown
  var createChildDropdown = function(i) {
  
    // Create a div in the following format:
    // <div class="childs">
    //    <label for="...">Child (number)</label>
    //    <select id="..."></select>
    // </div>
    var $childDropdown = $('<div />', {
      'class': 'childs'
    });
    $childDropdown.append($('<label />', {
      'for': 'childDropdown-' + i
    }).text('Child ' + i));
    $childDropdown.append($('<select />', {
      'id': 'childDropdown-' + i
    }));
    
    // Define options made available for each child
    var options = ['a', 'b', 'c'];
    options.forEach(function(option, index) {
//-> added parameter name index here ^^^^^ 
      // Create new option element and append to <select>
      $childDropdown.find('select').append($('<option />')
             .text(option).attr('value', index));
//-> and using it here ...               ^^^^^
    });
    
    // Return documentFragment so that we can append it
    return $childDropdown;
  };
  
  // Function to destroy child dropdown
  var destroyChildDropdown = function($el, i) {
    $el.find('div.childs').get(i).remove();
  };

  $(".button-click a").on("click", function() {
    var button = $(this);
    var oldVal = parseInt(button.closest("ul").prev().val());
    var newVal = (button.text() == "+") ? oldVal + 1 : (oldVal > 0) ? oldVal - 1 : 0;
    var total_value = "";

    button.closest("ul").prev().val(newVal);

    $(".travel").each(function() {
      var cat = $(this).prev('span').text();
      total_value += cat + ": " + $(this).val() + ", ";
    });

    if (oldVal < newVal) {
      $('.childDropdowns').append(createChildDropdown(newVal));
    } else if (oldVal > newVal) {
      destroyChildDropdown($('.childDropdowns'), newVal);
    }

    total_value = total_value.substring(0, total_value.length - 2);
    $(".main").val(total_value);
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label>
Count all1 Traveller(s)
<input type="text" name="no_travellers" class="main" value="Adults: 1" placeholder="" />
</label>
<br/>
<label>
   <span>Children</span>
   <input type="text" class="travel" id="CAT_Custom_410672" name="CAT_Custom_410672"  value="0" />
   <ul class="button-group button-click">
      <li><a href="#" class="small button secondary"><i class="fa fa-plus"><span class="hide">+</span></i></a></li>
      <li><a href="#" class="small button secondary"><i class="fa fa-minus"><span class="hide">-</span></i></a></li>
   </ul>
</label>
<div class="childDropdowns"></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download