0

I am having this array

 var serviceArray= new Array("Living Room","Dining Room","Bedroom(s)","Family Room","Kitchen","Den","Hallway(s)","Ste(s)","Bathroom","Landing(s)");

I want to bind all this array values in option of following select tag using jquery..

    <select class="services_list"></select>
1
  • What do you mean by "bind"? Do you want to create option elements? Commented Oct 16, 2014 at 7:37

4 Answers 4

1

Try this:

Demo on Fiddle

HTML:

<select class="services_list"></select>

JavaScript:

var serviceArray= new Array("Living Room","Dining Room","Bedroom(s)","Family Room","Kitchen","Den","Hallway(s)","Ste(s)","Bathroom","Landing(s)");

for (i = 0; i < serviceArray.length; i++) {
    var data = '<option>' + serviceArray[i] + '</option>'
    $('select').append(data);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Iterate your array and append option to your select element.

Heres Example:

var serviceArray = new Array("Living Room", "Dining Room", "Bedroom(s)", "Family Room", "Kitchen", "Den", "Hallway(s)", "Ste(s)", "Bathroom", "Landing(s)");

$(document).ready(function() {
  for (i = 0; i < serviceArray.length; i++) {
    $('.services_list').append('<option>' + serviceArray[i] + '</option>')
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="services_list"></select>

Comments

1

I'm assuming you want to create a select option list with the array so you can use .each() and .appendTo()

var serviceArray= new Array("Living Room","Dining Room","Bedroom(s)","Family Room","Kitchen","Den","Hallway(s)","Ste(s)","Bathroom","Landing(s)");
$(serviceArray).each(function(){
    $('<option>'+this+'</option>').appendTo('.services_list');
});

DEMO

Comments

1

Try this code

    var options = '';
    var serviceArray= new Array("Living Room","Dining Room","Bedroom(s)","Family Room","Kitchen","Den","Hallway(s)","Ste(s)","Bathroom","Landing(s)");
    for (var i = 0; i < serviceArray.length; i++) {
        options += '<option value="' + serviceArray[i] + '">' + serviceArray[i] + '</option>';
    }
    $('.services_list').html(options);

1 Comment

Performance wise generating string and then using html() is better.

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.