3

I want to assign the multiple selected values to the different text field. If i selected four option assign values to the four different text field.

Html code

<select name="garden" multiple="multiple">
  <option>Flowers</option>
  <option>Shrubs</option>
  <option>Trees</option>
  <option>Bushes</option>
  <option>Grass</option>
  <option>Dirt</option>
</select>
<div></div>
<input type="text" id="opt1" />
<input type="text" id="opt2"/>
<input type="text" id="opt3" />
<input type="text" id="opt4" />

What i did sample jquery code

<script>
$( "select" )
  .change(function() {
    var str = "";
    $( "select option:selected" ).each(function() {
      str += $( this ).text() + " ";
    });
    $( "div" ).text( str );  

    });
  })
  .trigger( "change" );
</script>

Please anybody help this. Thanks for your corporation

3 Answers 3

3

Generate id value based on the selected options index(you can get index value as the first argument of the callback function in each() method) and update its value by selecting element using the generated id.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="garden" multiple="multiple">
  <option>Flowers</option>
  <option>Shrubs</option>
  <option>Trees</option>
  <option>Bushes</option>
  <option>Grass</option>
  <option>Dirt</option>
</select>
<div></div>
<input type="text" id="opt1" />
<input type="text" id="opt2" />
<input type="text" id="opt3" />
<input type="text" id="opt4" />

<script>
  $("select").change(function() {
      var str = "";
      $("select option:selected").each(function(i) {
        str += $(this).text() + " ";
        $('#opt' + (i + 1)).val($(this).text());
      });
      $("div").text(str);
    })
    .trigger("change");
</script>


The much better approach would be using common class for inputs and you can get element by index using :eq() pseudo-class selector in jQuery.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="garden" multiple="multiple">
  <option>Flowers</option>
  <option>Shrubs</option>
  <option>Trees</option>
  <option>Bushes</option>
  <option>Grass</option>
  <option>Dirt</option>
</select>
<div></div>
<input type="text" class="opt" />
<input type="text" class="opt" />
<input type="text" class="opt" />
<input type="text" class="opt" />

<script>
  $("select").change(function() {
      var str = "";
      $("select option:selected").each(function(i) {
        str += $(this).text() + " ";
        $('.opt:eq(' + i + ')').val($(this).text());
      });
      $("div").text(str);
    })
    .trigger("change");
</script>


UPDATE : For limiting the number of selection always cache selected value in a global variable and restore when selected option count is greater than expected.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="garden" multiple="multiple">
  <option>Flowers</option>
  <option>Shrubs</option>
  <option>Trees</option>
  <option>Bushes</option>
  <option>Grass</option>
  <option>Dirt</option>
</select>
<div></div>
<input type="text" class="opt" />
<input type="text" class="opt" />
<input type="text" class="opt" />
<input type="text" class="opt" />

<script>
  var sel;
  $("select").change(function() {
      var str = "",
        $selected = $("select option:selected");

      if ($selected.length > 4)
        $(this).val(sel);
      else
        sel = $(this).val();

      $selected.each(function(i) {
        str += $(this).text() + " ";
        $('.opt:eq(' + i + ')').val($(this).text());
      });
      $("div").text(str);
    })
    .trigger("change");
</script>

Refer : Multiple Select limit number of selection

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

4 Comments

@Rijo : glad to help :)
@Rijo , If its proper solution.. you can mark it as Accepted answer.
one more help how can I limit maximum four selection
Thanks for your great help
1

Try like .Each time you select assign with different textbox . Increment the counter of each time click.It will match with the ids of textbox

var count=1;
$( "select" ).change(function() {
    var str = "";
    $( "select" ).each(function() {
      str += $( this ).val() + " ";
    });
      $( "div" ).text( str );  

       $('#opt'+count).val(str);
  count ++;
       
    });
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="garden" multiple="multiple">
  <option>Flowers</option>
  <option>Shrubs</option>
  <option>Trees</option>
  <option>Bushes</option>
  <option>Grass</option>
  <option>Dirt</option>
</select>
<div></div>
<input type="text" id="opt1" />
<input type="text" id="opt2"/>
<input type="text" id="opt3" />
<input type="text" id="opt4" />

2 Comments

This answer is also fine :)
Its only allow the 4 select .check the answer @Rijo
1

The solution is as,

using trigger in Jquery

Working Demo

 $("select option:selected").each(function(i) {
   if(i <= 4) {
      resVal += $(this).text() + " ";
      $('#opt' + (i + 1)).val($(this).text());
    }
  });

  $("div").text(resVal);

1 Comment

When I deselect how to reset all text field again

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.