473,412 Members | 5,361 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

default selected item and field value

Hi,

I have the following problem;

This is my form, i would like to default the select box to USA, and default
the text field to a name such as 'Sarah' and the hidden field to another
number like '876543', when the user clicks on the check box, otherwise the
fields should be blank, the value of the select box dosnt matter much, only
that it defaults to USA, when the user checks it. Can anyone help? i have
tried a google with no luck, anything would be appreciated.
--
<form name="form1">
<input type="checkbox" name="checkbox" value="checkbox">

<select name="prefix">
<option value="off" selected>Select country
<option value="61">Australia
<option value="1">USA
<option value="58">Venezuela
</select>

<input type="text" name="name">
<input type="hidden" name="number">
</form>
--
Sarah West
Jul 20 '05 #1
3 16163
Lee
Sarah West said:

Hi,

I have the following problem;

This is my form, i would like to default the select box to USA, and default
the text field to a name such as 'Sarah' and the hidden field to another
number like '876543', when the user clicks on the check box, otherwise the
fields should be blank, the value of the select box dosnt matter much, only
that it defaults to USA, when the user checks it. Can anyone help? i have
tried a google with no luck, anything would be appreciated.
--
<form name="form1">
<input type="checkbox" name="checkbox" value="checkbox">

<select name="prefix">
<option value="off" selected>Select country
<option value="61">Australia
<option value="1">USA
<option value="58">Venezuela
</select>

<input type="text" name="name">
<input type="hidden" name="number">
</form>
--


<html>
<head>
<script type="text/javascript">
var defaultValue={ prefix:2, name:"Sarah", number:876543 };
function setDefaults(box){
if(box.checked){
box.form.prefix.selectedIndex=defaultValue.prefix;
box.form.name.value=defaultValue.name;
box.form.number.value=defaultValue.number;
}else{ // clear the values if unchecked
box.form.prefix.selectedIndex=0;
box.form.name.value="";
box.form.number.value="";
}
}
</script>
<body>
<form name="form1">

<input type="checkbox"
name="checkbox"
value="checkbox"
onclick="setDefaults(this)">

<select name="prefix">
<option value="off" selected>Select country</option>
<option value="61">Australia</option>
<option value="1">USA</option>
<option value="58">Venezuela</option>
</select>

<input type="text" name="name">
<input type="hidden" name="number">

</form>
</body>
</html>

Jul 20 '05 #2
Thank you Lee, that works great.

I'm curious as to how it works?
box.form.name.value=defaultValue.name The name of my form is 'form1', and the name of my checkbox is called
'checkbox', how can you refer to it in such generic terms eg
'box.form.<element>.<value>'?

--
Sarah West

"Lee" <RE**************@cox.net> wrote in message
news:bo********@drn.newsguy.com... <html>
<head>
<script type="text/javascript">
var defaultValue={ prefix:2, name:"Sarah", number:876543 };
function setDefaults(box){
if(box.checked){
box.form.prefix.selectedIndex=defaultValue.prefix;
box.form.name.value=defaultValue.name;
box.form.number.value=defaultValue.number;
}else{ // clear the values if unchecked
box.form.prefix.selectedIndex=0;
box.form.name.value="";
box.form.number.value="";
}
}
</script>

Jul 20 '05 #3
"Sarah West" <ab*@hotmail.com> wrote in message
news:3f**********************@news.optusnet.com.au ...
I'm curious as to how it works?
box.form.name.value=defaultValue.name

The name of my form is 'form1', and the name of my checkbox
is called 'checkbox', how can you refer to it in such generic
terms eg 'box.form.<element>.<value>'?


box is a reference to a form element (the checkbox, the - this - object
within the onclick event handling method) and all form elements have a
property with the name "form" that is a reference to the form that
contains them.

So the onclick function passes the - setDefaults - function a reference
to the checkbox as - this -, the function receives that reference as
ts - box - parameter and can then refer to the containing form as -
box.form -, and can use that reference to the form exactly as it may use
any other reference to a form such as - document.forms['form1'] - .

Incidentally, "name" is not a good name for a form element as the form
object already has a property with the name "name" which holds the
string value provided in the HTML NAME attribute for the form ("form1").
Creating an element with the name "name" will result in the expected
string "name" property of the form being replaced with the reference to
the element. That is not a problem in this case as none of your code is
interested in the (original string) "name" property of the form, but
giving form elements NAME (or ID) attributes that correspond with
existing form element named properties is a habit that will eventually
come back and kick you. JavaScript is case sensitive and form property
names are entirely initial lower case so something as simple as always
making form element NAME attributes have initial capitals would be
sufficient to avoid any naming conflicts within the form.

Richard.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stanley J Mroczek | last post by:
I have tried this with no luck. What i want is to show the selected item on the first post. the selected item is NY but the first in the list shows as selected. If Page.IsPostBack = False Then...
5
by: Kris Rockwell | last post by:
Hello (again), I have gotten the dropdown list functionality to work through a few tricks (probably not the most efficient, but it works) but I am not sure how to set the default selected value....
2
by: huzz | last post by:
How do i make a dropdownlist selected value based on the value i retrive from the database. Basically i have an edit page and like to display the default value in a dropdown list from the...
3
by: John Walker | last post by:
Hi, On an ASP.NET page I have a drop down list control. When the user pulls down the list and makes a selection, I perform validation, and if the validation fails I want the selected item in...
2
by: B | last post by:
I'm trying to simply build a form with a combo box containing a list of states. I'd like for there to be NO default selected item, but invariably, the first item in the DataSource is being...
1
by: Ben | last post by:
I have a formview with a few dropdownlists (software version, database version, etc). When a software version is selected, the database version dropdownlist updates itself accordingly. When in...
9
nirmalsingh
by: nirmalsingh | last post by:
i have a combo box with id="combo1", i want to get the selected item or value through javascript. help me plz...
4
by: rn5a | last post by:
I am binding a DropDownList with records existing in a database table. I want to add an extra item *SELECT COMPANY* at index 0 so that by default, it gets selected. This is how I tried it but the...
6
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.