Current location: Hot Scripts Forums » General Web Coding » JavaScript » Want to change combo box using java script


Want to change combo box using java script

Reply
  #1 (permalink)  
Old 01-12-07, 01:30 AM
hYph3n hYph3n is offline
Newbie Coder
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Want to change combo box using java script

hi

I am using JSP to make some web. On one page, i have 2 combo boxes. say combo1, combo2. the combo1 values are taken from the db when page load and combo2 values are loaded depending on the combo1 selection (i am not using any db for combo2, it is just hardcoded value i have at client side so no need to send request).

What i want is to call a javascript function on combo1_selection_changed and update combo2, but i am unable to write the code for it. can someone help me out.
Reply With Quote
  #2 (permalink)  
Old 01-12-07, 05:41 AM
Vicious's Avatar
Vicious Vicious is offline
Community VIP
 
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
Hey,

I'm not sure if you ask how to do it in JSP or Javascript. Anyway, I can give you a quick hint in Javascript. If it's JSP you need, then I'm afraid I can't help you:

step 1: in the HTML of combo1, you set this attribute: onchange="combo1_selection_changed(this);"

step 2: create the function "combo1_select_changed(combo1)"

There you select the appropriate hard coded values. For each value you wish to append in the combo2 box, you create a new HTML element "option". You then set the value to the desired one, and the innerHTML to either the same value, or something more readable for your visitors. Then you append that option to the optionlist of combo2.

I could have written you a chunk of code, but I don't have enough information. And I also need to know if you meant Javascript or JSP. Something tells me that perhaps you're confusing these two.
Reply With Quote
  #3 (permalink)  
Old 01-12-07, 06:26 AM
hYph3n hYph3n is offline
Newbie Coder
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Vicious
Hey,

I'm not sure if you ask how to do it in JSP or Javascript. Anyway, I can give you a quick hint in Javascript. If it's JSP you need, then I'm afraid I can't help you:

step 1: in the HTML of combo1, you set this attribute: onchange="combo1_selection_changed(this);"

step 2: create the function "combo1_select_changed(combo1)"

There you select the appropriate hard coded values. For each value you wish to append in the combo2 box, you create a new HTML element "option". You then set the value to the desired one, and the innerHTML to either the same value, or something more readable for your visitors. Then you append that option to the optionlist of combo2.

I could have written you a chunk of code, but I don't have enough information. And I also need to know if you meant Javascript or JSP. Something tells me that perhaps you're confusing these two.
yeah i meant in JavaScript and it would be great if you give me some code. I have tried but some javascript object not found error and lot of others ...
Reply With Quote
  #4 (permalink)  
Old 01-12-07, 08:34 AM
Vicious's Avatar
Vicious Vicious is offline
Community VIP
 
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
Here's a complete testpage with the function you need. That should give you enough code to make it work for you:

Code:
<html>
	<head>
		<title>combo</title>
		<script language="javascript">
			var hardcoded_values = new Array();
			hardcoded_values[0] = "";
			hardcoded_values[1] = new Array("option 1 - part 1","option 1 - part 2","option 1 - part 3");
			hardcoded_values[2] = new Array("option 2 - part 1","option 2 - part 2");
			hardcoded_values[3] = new Array("option 3 - part 1","option 3 - part 2","option 3 - part 3","option 3 - part 4");
			
			
			
			function combo1_selection_changed(combo1)
			{
				// 1. get the selected value from combo1:
				var combo1_value = combo1.value;
				
				// 2. make sure combo2 is empty:
				document.forms["testForm"].elements["combo2"].options.length=0;
				
				// 3. loop throught the hard-coded values:
				for (var i=0;i<hardcoded_values[combo1_value].length;i++)
				{
					// dynamically create a new <option> element:
					var opt = document.createElement("option");
					// set the value-attribute of it:
					opt.setAttribute('value',i+1); // (normally, i is zero-based, but we do +1 so that "part 1" equals a value of 1..
					// set the displayed value:
					opt.innerHTML = hardcoded_values[combo1_value][i];
					// append this option to combo2:
					document.forms["testForm"].elements["combo2"].appendChild(opt);
				}
			}
		</script>
	</head>
	<body>
		<form name="testForm" action="" method="post">
			<select name="combo1" onchange="combo1_selection_changed(this);">
				<option value="0"></option>
				<option value="1">option 1</option>
				<option value="2">option 2</option>
				<option value="3">option 3</option>
			</select><br />
			<select name="combo2">
			</select>
		</form>
	</body>
</html>
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Raffle/Lottery Script (Very profitable!), Coded it myself. Voltaire General Advertisements 6 03-16-09 07:15 AM
Combo Box Search creepycridler PHP 1 09-14-05 08:55 AM
change drop down box based on textbox talax JavaScript 1 02-24-05 07:38 AM
Is there any integrity of script rankings? webmaster@atmanager.com Hot Scripts Forum Questions, Suggestions and Feedback 17 08-06-04 12:12 AM


All times are GMT -5. The time now is 11:22 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.