Select option box based on value of an input box in appended selectbox

inaz

I have a selectbox that shows number of childeren. when the user change selectbox it appends selectboxes for showing age of each child. moreover I have an input box with type readonly . how can I select each child age based on value of input type with classname age ?

i get values like this : 1,2

the first value is for the first selectbox and the second value is for second selectbox . and if user change select box froM the first i want to clear the selected option.

is there a way to do this ? here is my snippet :

$(document).ready(function(){
  $('.countRoom').find('.childnum').val($('.childcount').val());
  $('.countRoom').find('.childnum').change()
})
function childAge(a){
  $(a).each(function(){
    age=$(a).val();
    
    $(a).closest(".countRoom").find(".selectAge").empty();
    for(var j=1;j<=age;j++){
      $(a).closest(".countRoom").css("width","100%").find(".selectAge").append('<div class="age"><label>Age of Child' + j + '</label><select name="childage" class="childage form-control"><option value="1">Up to 1 years</option><option value="2">1 to 2 years</option></select></div>');
    }
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="countRoom">
     children
     <select onChange="childAge(this)" class="childnum">
      <option value="0"> 0 </option>
      <option value="1"> 1 </option>
      <option value="2"> 2 </option>
     </select>
   <input type="text" class="childcount" value="2" readonly style="display:none;">
    <input type="text" class="age" value="1,2" readonly style="background:green;">
     
    <div class="selectAge"></div>
   </div>

Vipin Kumar

First you have to get the value of input.age and then split it. After that you can use that value to set selected attribute in your loop

$(document).ready(function() {
  $('.countRoom').find('.childnum').val($('.childcount').val());
  $('.countRoom').find('.childnum').change()
})

function childAge(a) {
  var ageVals = $('input.age').val().split(',')
  $('input.age').val(',')
  $(a).each(function() {
    age = $(a).val();

    $(a).closest(".countRoom").find(".selectAge").empty();
    for (var j = 1; j <= age; j++) {
      $(a).closest(".countRoom").css("width", "100%").find(".selectAge").append('<div class="age"><label>Age of Child' + j + '</label><select name="childage" class="childage form-control"><option value="1" ' + (ageVals[j - 1] == 1 ? 'selected' : '') + '>Up to 1 years</option><option value="2" ' + (ageVals[j - 1] == 2 ? 'selected' : '') + '>1 to 2 years</option></select></div>');
    }
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="countRoom">
  children
  <select onChange="childAge(this)" class="childnum">
      <option value="0"> 0 </option>
      <option value="1"> 1 </option>
      <option value="2"> 2 </option>
     </select>
  <input type="text" class="childcount" value="2" readonly style="display:none;">
  <input type="text" class="age" value="1,2" readonly style="background:green;">

  <div class="selectAge"></div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select box option value checked

From Dev

Javascript function to check a box based on value of select option

From Dev

Javascript function to check a box based on value of select option

From Dev

second selectbox value changes on first select box change and vice versa

From Dev

Using a select option to populate input box with JQuery

From Dev

Change select box value depending other selected option box

From Dev

Clone Select Box Option value in same page

From Dev

How to filter select box based on value in preceding select box?

From Dev

Changing the value of Input field when user select option from select box

From Dev

Populate input text box based on drop down select box in Jquery

From Dev

Select option in drop down list based on text box input using jQuery

From Dev

Select Box option filter

From Dev

Select box value copy to input at clicking

From Dev

Dynamically add values to SELECT box, appended to body

From Dev

How to add new select box every time I change value of new created selectbox in jQuery?

From Dev

How to add new select box every time I change value of new created selectbox in jQuery?

From Dev

select value in select box other value in array will appear in the input field

From Dev

Disable/Enable a text box based on radio/select option selected Javascript

From Dev

change select box option based on variable's output

From Dev

Default Select Option Based on Input Value

From Dev

Angular select box with value differ from option text

From Dev

Checking wether or not select box option value contains a match

From Dev

Add Value To Input Box

From Java

Set selected option of select box

From Dev

Select box with first option empty

From Dev

Create a text box with a select option

From Dev

Select box and getting the option label

From Dev

Javascript search box with select option

From Dev

Select Box Option width Issue

Related Related

  1. 1

    Select box option value checked

  2. 2

    Javascript function to check a box based on value of select option

  3. 3

    Javascript function to check a box based on value of select option

  4. 4

    second selectbox value changes on first select box change and vice versa

  5. 5

    Using a select option to populate input box with JQuery

  6. 6

    Change select box value depending other selected option box

  7. 7

    Clone Select Box Option value in same page

  8. 8

    How to filter select box based on value in preceding select box?

  9. 9

    Changing the value of Input field when user select option from select box

  10. 10

    Populate input text box based on drop down select box in Jquery

  11. 11

    Select option in drop down list based on text box input using jQuery

  12. 12

    Select Box option filter

  13. 13

    Select box value copy to input at clicking

  14. 14

    Dynamically add values to SELECT box, appended to body

  15. 15

    How to add new select box every time I change value of new created selectbox in jQuery?

  16. 16

    How to add new select box every time I change value of new created selectbox in jQuery?

  17. 17

    select value in select box other value in array will appear in the input field

  18. 18

    Disable/Enable a text box based on radio/select option selected Javascript

  19. 19

    change select box option based on variable's output

  20. 20

    Default Select Option Based on Input Value

  21. 21

    Angular select box with value differ from option text

  22. 22

    Checking wether or not select box option value contains a match

  23. 23

    Add Value To Input Box

  24. 24

    Set selected option of select box

  25. 25

    Select box with first option empty

  26. 26

    Create a text box with a select option

  27. 27

    Select box and getting the option label

  28. 28

    Javascript search box with select option

  29. 29

    Select Box Option width Issue

HotTag

Archive