0

My problem is how do i populate my Birthday selectbox with data using jquery only? Is it possible to do it? Sorry but I'm just new to this...can anyone give me an example or something?

code:

<div class="form-group input-group">
    <div class="btn-group input-group">
        <span class="input-group-addon">Birthday</span>
        <select name="month" class="btn btn-default" id="bmonth" data-container="body" data-placement="left" data-toggle="popover" data-content="Select your Birthday!" >
            <option>Month</option>
        </select>
        <select name="day" class="btn btn-default" id="bday" data-container="body" data-placement="top" data-toggle="popover" data-content="Select your Birthday!" >
            <option>Day</option>
        </select>
        <select name="year" class="btn btn-default" id="byear" data-container="body" data-placement="right" data-toggle="popover" data-content="Select your Birthday!" >
            <option>Year</option>
        </select>
    </div>
Dyrandz Famador
  • 4,439
  • 5
  • 23
  • 39
user3671584
  • 339
  • 2
  • 3
  • 14

4 Answers4

2

Here is a fiddle

var months = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];

$(months).each(function(index,value){
   var curHtml = $('#bmonth').html();
   curHtml += '<option value="'+index+'">'+value+'</option>';
   $('#bmonth').html(curHtml);
});
J.Wells
  • 1,729
  • 12
  • 13
2

Try something like this (Example):

HTML

<select id='year'>
    <option>Year</option>
</select>

JS:

var years = [2010, 2011, 2012, 2013, 2014];
$.each(years, function(k, v){
    var option = $('<option/>', {'value':v, 'text':v});
    $('#year').append(option);
});

You got the idea, try the rest by yourself.

The Alpha
  • 138,380
  • 26
  • 281
  • 300
1

Here's more of a dynamic option that will change the maximum year for you every year. I set the minimum year to 1900, but you can change this if you want.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="Scripts/jQuery/jquery-2.1.0.min.js"></script>
    </head>
    <body>
        <form id="myForm"><select id="month"></select><select id="day"></select><select id="year"></select></form>
        <script>
            $(document).ready(function () {
                var month = [], day = [], year = [];
                for (var i = 1; i <= 12; i++) {
                    month.push(i);
                }
                for (var i = 1; i <= 31; i++) {
                    day.push(i);
                }
                for (var i = 1900; i <= (new Date().getFullYear()); i++) {
                    year.push(i);
                }
                $.each(day, function (index, d) {
                    $("#day").append("<option>" + d + "</option>");
                });
                $.each(month, function (index, m) {
                    $("#month").append("<option>" + m + "</option>");
                });
                $.each(year, function (index, y) {
                    $("#year").append("<option>" + y + "</option>");
                });
            });
        </script>
    </body>
</html>

http://jsfiddle.net/CfLKV/

Howard Renollet
  • 4,455
  • 1
  • 23
  • 31
0

You can follow the following code and create your own

<select name="month" id="bmonth">
    <option>Month</option>
</select>

Javascript

$(document).ready(function () {
    var months = [
        'Jan', 'Feb', 'Mar', 'Apr',
        'May', 'Jun', 'Jul', 'Aug',
        'Sep', 'Oct', 'Nov', 'Dec'];
    for (var i = 0; i < months.length; i++) {
        $("#bmonth").append(GetOption(months[i], months[i]));
    }
});

function GetOption(text, value) {
    return "<option value = '" + value + "'>" + text + "</option>";
}

Demo

Krishanu Dey
  • 6,212
  • 6
  • 50
  • 67