Jide Igun
Jide Igun

Reputation: 3

displaying Multiple select option in diiferent line

I have a multiple select option that display the result in a div container, when click on ctrl+"orange""apple""banana" the result display: "orange, apple, banana" in one line, but i want to display each result in a new single line with a link that goes to different page like this:

Orange - "goes to a link"
Apple - "goes to a link"
Banana - "goes to a link"

Here are my codes below:

<script src="jquery-mobile/jquery-1.8.3.min.js" type="text/javascript">

</script>
<select name="" multiple="multiple" id="select-custom-19">    
    <option>Add Fruits</option>        
    <option value="orange" selected>orange</option> 
    <option value="apple">apple</option>
    <option value="banana">banana</option>             </select>
<div style="width:300px; height:70px; background-color:#E1D903; 
        color:#000000; margin: 10px; "> <span id="yourfruit"> </span>

</div>
<script type="text/javascript">
    $(document).ready(function() {

        $('#select-custom-19').change(function() {
            /* setting currently changed option value to option variable */
            var option = $(this).find('option:selected').val();
            /* setting input box value to selected option value */
            $('#yourfruit').text($(this).val());
        });
    });
</script>

your help will be highly appreciated.

Thanks

Upvotes: 0

Views: 586

Answers (4)

adricadar
adricadar

Reputation: 10209

You can iterate throug items and display them, append an anchor (<a />) and use <br /> for a new line.

Make sure to add .empty() to clean the fruits from list before .append() other items to $('#yourfruit'), like in example below.

var fruits = $(this).val();
$('#yourfruit').empty();
for (var fruit in fruits) {
   var label = $('<label/> ').text(fruits[fruit]+" - ");
   $('#yourfruit').append(label)
                   .append($('<a  class="tab-btn" />').text(fruits[fruit]).attr('href', fruits[fruit] + '.html'))
                  .append('<br />');
}

$(document).ready(function() {
  $('#select-custom-19').change(function() {
    /* setting currently changed option value to option variable */
    var option = $(this).find('option:selected').val();
    /* setting input box value to selected option value */
    var fruits = $(this).val();
    $('#yourfruit').empty();
    for (var fruit in fruits) {
        var label = $('<label/> ').text(fruits[fruit] + " - ");
        $('#yourfruit').append(label)
                       .append($('<a  class="tab-btn" />').text(fruits[fruit]).attr('href', fruits[fruit] + '.html'))
                       .append('<br />');
    }

  });
});
.tab-btn {
  color: red;
}
.tab-btn:hover {
  color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" multiple="multiple" id="select-custom-19">
  <option>Add Fruits</option>

  <option value="orange" selected>orange</option>
  <option value="apple">apple</option>
  <option value="banana">banana</option>

</select>

<div style="width:300px; height:70px; background-color:#E1D903; 
    color:#000000; margin: 10px; ">
  <span id="yourfruit"> </span>
</div>

Upvotes: 0

Mox Shah
Mox Shah

Reputation: 3015

You may add HTML tags as per your requirements, like <a> or </br>

$(document).ready(function () {
    $('#select-custom-19').change(function () {
        $('#yourfruit').text("");
        if($(this).val() != null)
        {
           $(this).val().forEach(function (value, index) {
               $('#yourfruit').append("<a href='#'>" + value + "</a></br>");
           });
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<select name="" multiple="multiple" id="select-custom-19">
    <option>Add Fruits</option>
    <option value="orange" selected>orange</option>
    <option value="apple">apple</option>
    <option value="banana">banana</option>
</select>
<div style="width:300px; height:70px; background-color:#E1D903; 
    color:#000000; margin: 10px; "> <span id="yourfruit"> </span>

</div>

Upvotes: 0

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

You can try adding <br/> element after every selected option. I have used <label> element but you can add link or any other element you want

$(document).ready( function ()
     {
     $('#select-custom-19').change(function(){

        $('#yourfruit').empty();

        var values = $(this).val();

        for(var i=0; i < values.length ; i++)
        {
          $('#yourfruit').append('<lable>'+values[i]+'</label><br/>');
        }   
      });
 });

JSFiddle Demo

Upvotes: 1

Anto S
Anto S

Reputation: 2449

try below

<script src="jquery-mobile/jquery-1.8.3.min.js" type="text/javascript"></script>
<select name="" multiple="multiple" id="select-custom-19">
  <option>Add Fruits</option>
  <option value="orange" selected>orange</option>
  <option value="apple">apple</option>
  <option value="banana">banana</option>
</select>
<div style="width:300px; height:70px; background-color:#E1D903;  color:#000000; margin: 10px; ">
  <ul id="yourfruit">
  </ul>
</div>
<script type="text/javascript">
    $(document).ready( function ()
     {

           $('#select-custom-19').change(function()
           {
        /* setting currently changed option value to option variable */
        var option = $(this).find('option:selected').val();
        /* setting input box value to selected option value */
        $('#yourfruit').append('<li>'+$(this).val()+'</li>');
            }
              );
                 });
    </script>

Upvotes: 0

Related Questions