Solved

How to show multiple options in dropdown and not one option

Posted on 2015-01-11
2
72 Views
Last Modified: 2015-01-11
I have a select that is populating but I need it to list all number of days instead of the total of days. For example if its 3 days  I need the dropdown to have option 1 day, option 2 days , option 3 days and not just option 3 days

var selectedDate = new Date();
				$( "#checkout" ).datepicker({
					dateFormat: 'yy-mm-dd',
					defaultDate: "+1w",
					changeMonth: false,
					numberOfMonths: 1,
					showOn: 'button',
					buttonText: '<i class="calendar"></i>',					
					prevText: '<i class="chevron-left"></i>',
					nextText: '<i class="chevron-right"></i>',			
				});
				$("#checkout").datepicker("option", "minDate", selectedDate);


$('#checkout').datepicker().bind("change", function () {
    calculate();
});

function calculate() {
    var d1 = new Date();
    var d2 = $('#checkout').datepicker('getDate');
    var diff = 1;
    if (d1 && d2) {
        diff = diff + Math.floor((d2.getTime() - d1.getTime()) / 86400000); // ms per day
		
	var QTY = [diff];    
    var sel = document.getElementById('duration');
for(var i = 0; i < QTY.length; i++) {
    var opt = document.createElement('option');
    opt.innerHTML = QTY[i];
    opt.value = QTY[i];
    sel.appendChild(opt);
}
    }
}

Open in new window

0
Comment
Question by:ucohockey
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40543657
Try changing lines 26-33 to:
        var sel = document.getElementById('duration');
        sel.innerHTML = ""; // clear
        for(var i = 0; i < diff; i++) {
            var opt = document.createElement('option');
            opt.innerHTML = (i+1) + ' day' + (i>0?'s':''); // opt.text
            opt.value = i+1;
            sel.appendChild(opt);
        }

Open in new window

Note that the option value and text are different (on purpose). I made a jsFiddle, when you click submit you see the form values: http://jsfiddle.net/robert_schutt/heamy7LL/1/
0
 

Author Closing Comment

by:ucohockey
ID: 40543660
Works perfect thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
update field on focusout 15 23
Phone Dialer 5 36
What does this javascript do? 7 24
JavaScript Scope issue 4 20
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now