Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to show multiple options in dropdown and not one option

Posted on 2015-01-11
2
76 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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…

860 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