kreya
kreya

Reputation: 1229

How to define dropdowns options in general so that we can assign it another one

I have more than 10 drop-downs.They all contain same value. I dont want to define its options for more than 10 times.So is there any way we can define it one time only ?I mean is there any way we can also define it in general way ?

e.g

<select id='monday_1'></select>
<select id='monday_2'></select>
<select id='tuesday_1'></select>
<select id='tuesday_2'></select>

Here its options like 01:00 AM ,01:15 AM,01:30 AM etc ... Is there any way we can defined it in general way so that we can assign it to all drop-downs.

Upvotes: 0

Views: 34

Answers (1)

user2575725
user2575725

Reputation:

With Jquery:

$(function() {
  var time = ['01:00 AM', '01:15 AM', '01:30 AM'];
  $('select.time').html('<option>' + time.join('</option><option>') + '</option>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id='monday_1' class='time'></select>
<select id='monday_2' class='time'></select>
<select id='tuesday_1' class='time'></select>
<select id='tuesday_2' class='time'></select>

Without Jquery:

var time = ['01:00 AM', '01:15 AM', '01:30 AM'];
var html = '<option>' + time.join('</option><option>') + '</option>';
var dd = document.getElementsByClassName('time');
var l = dd.length;
while (l--)
  dd[l].innerHTML = html;
<select id='monday_1' class='time'></select>
<select id='monday_2' class='time'></select>
<select id='tuesday_1' class='time'></select>
<select id='tuesday_2' class='time'></select>

Upvotes: 1

Related Questions