Solved

Jquery Dynamically Selected DropDown field

Posted on 2014-01-24
3
901 Views
Last Modified: 2014-01-24
Trying to work this logic out but I'm stuck. I have two dropdown menus. First one is "Number of Rooms" the 2nd one is "Number of Guests". Depending on how many rooms one selects will determine the minimum and maximum number of guest are allowed per room. Each room allows a minimum of 1person and a max of 4 persons. So if I selected 4 Rooms my min guest would be 4 and max would 16.

The logic for the drop downs is:
1 Room: 1, 2, 3 OR 4 Guests
2 Rooms: 2, 3, 4, 5, 6, 7, or 8 Guests
3 Rooms: 3, 4, 5, 6, 7, 8, 9, 10, 11 or 12 Guests
4 Rooms: 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 or 16 Guests

Now how do I make the minimum number of guests the default selected state?
So if the user selects 3 rooms then I would like to make 3 Guests the default selected state even though the max could be 12.

I'm thinking:
 $('select option:nth(0)').attr("selected", "selected");
or
var myDDL = $('#solutionId');
myDDL[0].selectedIndex = 0;
but I couldn't get them to work.

My Code:
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
var combinations = {
    "1": ["1","2","3","4"],
	"2": ["2","3","4","5","6","7","8"],
	"3": ["3","4","5","6","7","8","9","10","11","12"],
	"4": ["4","5","6","7","8","9","10","11","12","13","14","15","16"]
}


$('#num_rooms').change(function(){
    var template = '<option>Choose Guests</option>';  // will hold html for second drop down.

    solutions = combinations[$(this).val()];
	
	
   $.each(solutions, function(i, solution){
        template += '<option value="' + solution + '">' + solution + ' Guests</option>'
    });
	
	$('#solutionId').html(template);
});

$('#solutionId').change(function(){
    yourtext = $(this).val();  // change yourtext to whatever you like.
   
});
});//]]>  
 

<form action="somepage.asp" method="get"> 

<select id="num_rooms" name="Rooms">
    <option value="1">1 Room</option>
    <option value="2">2 Rooms</option>
    <option value="3">3 Rooms</option>
    <option value="4">4 Rooms</option>
</select>

<select id="solutionId" name="Guests">
    <option value="1">1 Guest</option>
    <option value="2" selected="">2 Guests</option>
    <option value="3">3 Guests</option>
   <option value="4">4 Guests</option>
</select>

<input type="submit">
</form>

Open in new window

0
Comment
Question by:swaggerking
3 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39807009
http://jsfiddle.net/GaryC123/ELE5q/

$("#num_rooms").change(function(){
  $("#solutionId").val(this.value)
})


(315 points - how do you come up with that?)
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 315 total points
ID: 39807354
Have a look at this fiddle:

http://jsfiddle.net/ChrisStanyon/wxp8H/

When you change the Rooms, it will rebuild the Guest dropdown with the correct minimum and maximum number of guests :)
0
 

Author Closing Comment

by:swaggerking
ID: 39807445
Nice and clean. Worked out of the box.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 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

19 Experts available now in Live!

Get 1:1 Help Now