473,398 Members | 2,368 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,398 software developers and data experts.

Setting forms using Javascript

Hi All,

Been learning Javascript (via google) to create forms with pre-set values.
As seen below. My problem I have found is this: I can't seem to figure out
(or find so far via google) how to preset form options when the input type
is a radio button, or dropdown list. The same method does not seem to work
with input fields.

Anyone know of such a comprehensive site?

Thanks ahead,

Daniel

<FORM name="PersProf" action="newuser.pl" method="POST">

<table border="0">
<tr>
<td WIDTH="200">
First Name
</td>
<td>
<input type="text" name="first_name" maxlength=32 size = 32>
</td>
</tr>
</table>
<script language="JavaScript">
<!--
//setting the value:
document.PersProf.first_name.value = '$first_name';
-->
</script>
Jul 23 '05 #1
12 1934
daniel kaplan wrote:
My problem I have found is this: I can't seem to figure out
(or find so far via google) how to preset form options when the input type
is a radio button, or dropdown list. The same method does not seem to work
with input fields.


Do you mean preselected:
<input type="radio" checked> and
<option selected value="val">text</option>
Jul 23 '05 #2
On Sat, 11 Dec 2004 09:45:59 -0500, daniel kaplan <no****@nospam.com>
wrote:
Hi All,

Been learning Javascript (via google) to create forms
with pre-set values.
You don't specify initial values with Javascript. You specify them in HTML:

<input type="text" ... value="initial value">

<input type="checkbox" ... checked>

<input type="radio" ... checked>

<select ...>
<option ... selected>...</option>
</select>

<textarea ...>initial value</textarea>

I suggest you read the HTML Specification
(<URL:http://www.w3.org/TR/html4/>), rather than rely on the Web.
Information regarding forms can be found under section 17
(<URL:http://www.w3.org/TR/html4/interact/forms.html>).

If you have any further problems, please ask in alt.html or
comp.infosystems.www.authoring.html.

[snip]
<script language="JavaScript">
The language attribute has been deprecated for over six years. Use the
type attribute instead:

<script type="text/javascript">
<!--
Hiding scripts with SGML comment delimiters is an obsolete practice. It's
not necessary.
//setting the value:
document.PersProf.first_name.value = '$first_name';


The preferred way to access form controls is via the forms and elements
collections:

document.forms['form-id'].elements['control-name-or-id']

[snip]

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsium76g8x13kvk@atlantis...

You don't specify initial values with Javascript. You specify them in

HTML:

i guess since i was able to set the initial value of a TEXT field using
javascript, that i could do the same with ALL fields in the form...

and i using perl to generate the HTML dynamically, so my perl build the
html, with javascript included...

i can set them using HTML, but to do it dynamcially i turned to java....

strange that it allows me to do it for text and nothing else, no?

daniel
Jul 23 '05 #4
Ivo
"daniel kaplan" wrote
i guess since i was able to set the initial value of a TEXT field using
javascript, that i could do the same with ALL fields in the form...
Being able to do something is never a good reason to go do it. In
programming the question should be: is it necessary? Here the answer is a
crystal clear no, see below.
and i using perl to generate the HTML dynamically, so my perl build the
html, with javascript included...

i can set them using HTML, but to do it dynamcially i turned to java....
Java? I didn't see any java in your earlier code. It 's javascript, a
totally different and independent language. The names are confusing,
no one disagrees with that, but the environments in which they operate,
are incomparable, even within the same webbrowser.
strange that it allows me to do it for text and nothing else, no?


It does allow you to do it for all types of input elements. The point is
that you don't need javascript at all. Let your perl script write the values
directly in the value attributes of the elements. From your first post I
gather that in the perl syntax it would look like this:

<input type="text" name="first_name" value="$first_name">

Look Ma, no javascript!
--
Ivo
http://www.vansandick.com/


Jul 23 '05 #5
"Ivo" <no@thank.you> wrote in message
news:41**********************@news.wanadoo.nl...
Being able to do something is never a good reason to go do it. well, it seemed that javascript was rich Forms features. of course am
totally new to it, so that is how it appears to me, it coudl be wrong

Java? I didn't see any java in your earlier code. obviously i typo'ed there
It does allow you to do it for all types of input elements. do you know a decent website which points this out? even if i don't end up
using that part of it, i would still like to learn it.

The point is that you don't need javascript at all. Let your perl script write the values directly in the value attributes of the elements. From your first post I
gather that in the perl syntax it would look like this:

<input type="text" name="first_name" value="$first_name">


that is how i started...but when it came to other forms features such as a
drop down list of states, where one might already be sleceted, it obviously
was going to be much more coding. i.e. (OPTION vs. SELECTED) so that's why
i turned to look at javascript.

thanks,

daniel
Jul 23 '05 #6
daniel kaplan wrote:
"Ivo" <no@thank.you> wrote in message
news:41**********************@news.wanadoo.nl...

The point is that you don't need javascript at all. Let your perl script
write the values directly in the value attributes of the elements. From
your first post I gather that in the perl syntax it would look like this:

<input type="text" name="first_name" value="$first_name">

that is how i started...but when it came to other forms features such as a
drop down list of states, where one might already be sleceted, it obviously
was going to be much more coding. i.e. (OPTION vs. SELECTED) so that's why
i turned to look at javascript.


Hava Perl generate the select element. As it loops through the elements
of an array to create it, have it check the value, if they match, then
add the selected.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #7
"Randy Webb" <Hi************@aol.com> wrote in message
news:vM********************@comcast.com...
Hava Perl generate the select element. As it loops through the elements
of an array to create it, have it check the value, if they match, then
add the selected.


well i am doing that with a lot of Ternery statements, because i am using
perl to build out the entire HTML page...it's just messy, so i assumed that
using javascript to do it at the end would have been cleaner
Jul 23 '05 #8
Ivo
"Randy Webb" wrote
daniel kaplan wrote:"
"Ivo" <no@thank.you> wrote
Being able to do something is never a good reason to go do it. well, it seemed that javascript was rich Forms features. of course am
totally new to it, so that is how it appears to me, it coudl be wrong


The rule applies to all things. As far as javascript is concerned, you 'll
stop being new after reading the FAQ of this newsgroup
http://www.jibbering.com/faq/

and related documents, and you understand why the sport is to rely on
javascript as little as possible. You control what happens on the server,
but when a page reaches the client, you just don't know what browser on what
OS is claiming to be some other browser, scripts may even be rewritten by
proxies and plugins (think popup blockers) and by the user himself. Most
importantly, a lot of people surf with javascript disabled or simply not
available, by choice or otherwise (estimates vary between 5%-25%). So you
want your page to be complete when you send it, and use javascript only for
things that are impossible without javascript.
Hava Perl generate the select element. As it loops through the elements
of an array to create it, have it check the value, if they match, then
add the selected.


Here is how I do them in PHP:

<?
function writeselect($name,$atb='',$ops=array(),$sel=''){
$s='<select name="'.$name.'" id="'.$name.'" '.$atb.'>';
$t=0; foreach($ops as $n=>$v) { $s.='<option
value="'.($t===$n?$v:$n).'"'.(($v==$sel)?' selected':'').'>'.$v; $t++;}
return $s.'</select>';
}
?>
<label for="cat" title="Grouping by topic">Category: </label>
<?=writeselect('cat','class="put"',array('Art', 'Business',
'Computing', 'General', 'Travel'),'General');?>
<?=writeselect('amount','onchange="foo(this);"',ar ray('Amount', 1=>'One',
12=>'Dozen'));?>

--
Ivo
http://4umi.com/web/javascript/



Jul 23 '05 #9
daniel kaplan wrote:
"Randy Webb" <Hi************@aol.com> wrote in message
news:vM********************@comcast.com...

Hava Perl generate the select element. As it loops through the elements
of an array to create it, have it check the value, if they match, then
add the selected.

well i am doing that with a lot of Ternery statements, because i am using
perl to build out the entire HTML page...it's just messy, so i assumed that
using javascript to do it at the end would have been cleaner


As Ivo has said, never rely on JS. Use it as an addition but never a
requirement.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #10
"Randy Webb" <Hi************@aol.com> wrote in message
news:Ac********************@comcast.com...
daniel kaplan wrote: As Ivo has said, never rely on JS. Use it as an addition but never a
requirement.


thanks all for the advice...

daniel
Jul 23 '05 #11
JRS: In article <11***************@nntp.acecape.com>, dated Sat, 11 Dec
2004 14:18:40, seen in news:comp.lang.javascript, daniel kaplan
<no****@nospam.com> posted :
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsium76g8x13kvk@atlantis...

You don't specify initial values with Javascript. You specify them in

HTML:

i guess since i was able to set the initial value of a TEXT field using
javascript, that i could do the same with ALL fields in the form...


You cannot set the initial value of an HTML text field with javascript;
you can only replace it - before the user notices!

The same goes for other controls.

AFAIK, in javascript you can re-set the value of any identifiable
control that exists. Even an isolated radio-button can, ISTM, be cleared
by code.
// // // document.write("<input type=radio name=X> Do Not Touch") // !!

In javascript, you can output HTML, including creation of controls with
computed initial values; but that is not quite the same thing.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #12
daniel kaplan wrote:
well i am doing that with a lot of Ternery statements, because i am using
perl to build out the entire HTML page...it's just messy


Try installing Template::Toolkit, it makes things rather cleaner.

In the Perl you would build a data structure (possibly from a database) that
looks something like:

$myHashRef = {
selectItems => {
one => {
value => 1,
label => 'Thingy',
},
two => {
value => 'wibble',
label => 'Wibble',
},
foo => {
value => 3,
label => 'Baz',
select => 1,
},
},
};

and include something like this in the template:

<select name="mySelect">
[% foreach item = selectItems %]
<option value="[% item.value %]"[% IF item.select %] selected[% END %]>
[% item.label %]
</option>
[% END %]
</select>

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jul 23 '05 #13

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

Similar topics

21
by: | last post by:
Hi, I am setting the NumericUpDown .Value property and the ValueChanged event is NOT being fired. Does this ONLY get fired when I change it on the UI and not programatically? Thanks
0
by: DotNetDummy | last post by:
Hi all, I am trying to set the printing setting e.g duplex mode etc. on a default printer when I.E object started printing a particular html doc. Here's the partial code, any help would be...
2
by: dsnyder | last post by:
This HTML has a bit of Javascript at the end that puts the initial focus on the userID field. It works great on Windows2000 running IE6, but the initial focus never goes to the userID field on...
2
by: Hasan Ammar | last post by:
Is it possible to set up hotkeys using onkeypress? I know it can be done with the usual alphanumeric keys, but what about function keys? or using ctrl/alt combinations? Does anybody have a...
3
by: Bob | last post by:
I'm a total newbie at Javascript, but a programmer for over 20 years... so, my plans may be bigger than they are possible. I'm trying to set with Javascript the value of a form field... easy...
0
by: Shravan | last post by:
Hi, I have a Windows Forms Custom DataGrid, which is put in a usercontrol, which on setting DataSource is setting focus to grid. The call stack for setting the focus is as follows. This is not...
3
by: Andy Fish | last post by:
Hi, I want to generate a value with javascript and include it in the form postback for a web form. based on my previous web development experience, I think I need an <input type=hidden> on...
2
by: jason | last post by:
Hello and Good Day. REALLY LOST. Running ASP.NET 1.1 Becuase I think I'm using my own controls smartnavigation does not appear to work for me. Stardard issue: I've got a datagrid thats...
4
by: PJ6 | last post by:
From a control's onkeypress the below javascript snippet is executed. I am attmpting to set the value of a hidden field... I believe I'd be able to get the contents of the hidden field by using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...

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.