Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-12-2012, 05:45 PM   PM User | #1
mtcbells
New to the CF scene

 
Join Date: Nov 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
mtcbells is an unknown quantity at this point
Help getting value from a drop down field

I have a form where I am collecting information from prospective clients. One of the fields is "County" wherein I have listed all the counties that I provide services, and the cost for that county. When they choose their county from the drop down list, I would like a hidden field called "County_Cost" to automatically be populated with the correct amount. Even if they go back and choose a different county, that field should update. That way, when they're done entering all their information, and they click submit, a contract is sent to them with the correct total.

In my drop down list, I currently have each county listed like "Wake,46" and then another county is "Franklin,75" and so on. The number after the comma is obviously, the cost for that county.

Any help would be greatly appreciated. I'm not that good at Java and just don't have time this week to do the research.
mtcbells is offline   Reply With Quote
Old 11-12-2012, 06:10 PM   PM User | #2
Brandnew
New Coder

 
Join Date: Aug 2012
Posts: 50
Thanks: 0
Thanked 11 Times in 11 Posts
Brandnew is an unknown quantity at this point
Are you asking this for Java or JavaScript?
__________________
1 Corinthians 15:3-4 / Ephesians 2:8-9 - What or Who are you living for?
Brandnew is offline   Reply With Quote
Old 11-12-2012, 06:58 PM   PM User | #3
Riku
New Coder

 
Join Date: Feb 2012
Location: Finland
Posts: 57
Thanks: 3
Thanked 9 Times in 9 Posts
Riku is an unknown quantity at this point
Something like this?

Code:
<script src="http://code.jquery.com/jquery-latest.js"></script>

<select>
<option id="country_and_cost">Select country</option> 
<option value="27">Argentina</option> 
<option value="95">Australia</option> 
<option value="150">Austria</option>
</select>

<input type="text" id="cost" readonly="readonly" value="" />

<script type="text/javascript">
// <![CDATA[
	$(document).ready( function() { 
	var selection = 0;
	$('select').change(function() {
	selection = $(this).val();
	$('#cost').val('Cost = ' + selection);
	});
	})
// ]]>
</script>
Hope this helps.

-Riku

Last edited by Riku; 11-12-2012 at 06:59 PM.. Reason: for got to wrap my code around CODE tags, love <3
Riku is offline   Reply With Quote
Old 11-12-2012, 07:05 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Assuming that you mean Javascript (a totally different language to Java in spite of the confusingly similar names):-

Code:
<select id = "counties" onchange = "showvalue()">
<option value = "0"> Chose a county</option>
<option value = "46">Wake</option>
<option value = "75">Franklin</option>
</select>
<br><br>
<input type = "text" id = "County_Cost">

<script type = "text/javascript">

function showvalue() {
document.getElementById("County_Cost").value = ""; // reset the field
var val = document.getElementById("counties").value;
if (val != 0) {
document.getElementById("County_Cost").value = val;
}
}

</script>
Change the County_Cost field to hidden after testing.

Riku's code uses jQuery. You did not specify that, and in any case it is silly to download the large overhead of a Javascript framework to accomplish something so simple.

The value of a select list is simply obtained with
var val = document.getElementById("counties").value;
No need for any reference to selectedIndex.

Quizmaster: Which surname was shared by a historical outlaw called "Butch" and a fictional cowboy called "Hopalong"?
Contestant: Lesbian.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-12-2012 at 07:12 PM..
Philip M is offline   Reply With Quote
Old 11-12-2012, 07:05 PM   PM User | #5
Brandnew
New Coder

 
Join Date: Aug 2012
Posts: 50
Thanks: 0
Thanked 11 Times in 11 Posts
Brandnew is an unknown quantity at this point
Got this idea from http://www.codeproject.com/Articles/...-list-selectio

Code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

function update(selection){
var choice = selection.selectedIndex;
document.getElementById("price").innerHTML="Pricing for County is " + selection.options[choice].value;
}

</script>
</head>

<body>
<p id="price" >Select county for pricing</p> 

<form>

<select id="hi" onChange="update(this.form.hi);">
  <option>Select County</option>
  <option value="25.00">This Location</option>
  <option value="50.00">That Location</option>
  <option value="75.00">Some Location</option>
</select>

</form>
</body>
</html>
__________________
1 Corinthians 15:3-4 / Ephesians 2:8-9 - What or Who are you living for?
Brandnew is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:14 PM.


Advertisement
Log in to turn off these ads.