473,387 Members | 1,501 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,387 software developers and data experts.

What am I doing wrong?

I'm not a javascripter so this is slightly unfamiliar to me. What I'm
trying to do is that if someone clicks the radio button for Other, then
the text input for other will become enabled.

What is wrong with this? Thank you!

<html>
<head>
<script language="JavaScript">
<!--
function Package(thisform) {

if (thisform.package[3].checked) {
thisform.otherPackage.disabled = false
} else {
thisform.otherPackage.disabled = true
}

}
-->
</script>
</head>

<body>

<form name="shipForm">
<input type="text" name="number" size="5">
<input type="radio" name="package"
onClick=Package(shipForm)>Pallet</span>
<input type="radio" name="package" onClick=Package(shipForm)>Box</span>
<input type="radio" name="package"
onClick=Package(shipForm)>Envelope</span>
<input type="radio" name="package"
onClick=Package(shipForm)>Other</span>
<input type="text" name="otherPackage" disabled>
</form>

</body>
</html>

Dec 29 '05 #1
9 1490
I figured it out. :) No need for a reply.

Dec 29 '05 #2
<ki**********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
I'm not a javascripter so this is slightly unfamiliar to me. What I'm
trying to do is that if someone clicks the radio button for Other, then
the text input for other will become enabled.

What is wrong with this? Thank you!

<html>
<head>
<script language="JavaScript">
<!--
function Package(thisform) {

if (thisform.package[3].checked) {
thisform.otherPackage.disabled = false
} else {
thisform.otherPackage.disabled = true
}

}
-->
</script>
</head>

<body>

<form name="shipForm">
<input type="text" name="number" size="5">
<input type="radio" name="package"
onClick=Package(shipForm)>Pallet</span>
<input type="radio" name="package" onClick=Package(shipForm)>Box</span>
<input type="radio" name="package"
onClick=Package(shipForm)>Envelope</span>
<input type="radio" name="package"
onClick=Package(shipForm)>Other</span>
<input type="text" name="otherPackage" disabled>
</form>

</body>
</html>


Try this instead.

<html>
<head>
<title>Package.htm</title>
<script type="text/javascript">
function Package(thisform) {
if (thisform.package[3].checked) {
thisform.otherPackage.style.backgroundColor = "#FFFFFF";
thisform.otherPackage.disabled = false;
thisform.otherPackage.focus();
} else {
thisform.otherPackage.value = "";
thisform.otherPackage.style.backgroundColor = "#DDDDDD";
thisform.otherPackage.disabled = true;
}
}
</script>
</head>
<body>
<form name="shipForm">
<input type="text" name="number" size="5">
<input type="radio" name="package"
onClick="Package(shipForm)">Pallet
<input type="radio" name="package"
onClick="Package(shipForm)">Box
<input type="radio" name="package"
onClick="Package(shipForm)">Envelope
<input type="radio" name="package"
onClick="Package(shipForm)">Other
<input type="text" name="otherPackage"
disabled style="background-color: #DDDDDD">
</form>
</body>
</html>
Dec 29 '05 #3
Thanks McKirahan. This is something I can learn from. :)

Dec 29 '05 #4
McKirahan wrote:
<html>
<head>
<title>Package.htm</title>
Not Valid. <URL:http://validator.w3.org/>
<script type="text/javascript">
function Package(thisform) {
Identifiers of methods not intended to be used as constructors should not
start with a capital letter, not even to work around a reserved word issue.

function _package(inp)
{
if (thisform.package[3].checked) {
Should be

if (thisform.form.elements['package'][3].checked)

aso. `thisform' is a reference to the HTMLFormElement, but that should
not be passed with `shipForm' (it's IE-proprietary); instead, `this' should
be passed and the `form' property of the referenced event target object
should be used. Form controls should be referenced through the `elements'
collection when possible (as here).
thisform.otherPackage.style.backgroundColor = "#FFFFFF";
<URL:http://www.w3.org/QA/Tips/color>
thisform.otherPackage.disabled = false;
thisform.otherPackage.focus();
<URL:http://pointedears.de/scripts/test/whatami#inference>
} else {
thisform.otherPackage.value = "";
thisform.otherPackage.style.backgroundColor = "#DDDDDD";
thisform.otherPackage.disabled = true;
Since `thisform.otherPackage' is used very often, it is prudent to assign
the reference to a variable and use the latter in place once it has been
established that it stores a valid reference:

var o = thisform.otherPackage;
if (o)
{
o.... = ...
}

or, if the recommendation above is followed:

var f, o;
if (inp
&& (f = inp.form)
&& (o = f.elements)
&& (o = o['otherPackage']))
{
var b = f.elements['package'][3].checked;
if (!b)
{
o.value = "";
}

o.style.backgroundColor = b ? '#fff' : '#ccc';
o.style.color = b ? '#000' : '#333';
o.disabled = b;

if (b && isMethodType(typeof o.focus))
{
o.focus();
}
}
}
}
</script>
</head>
<body>
<form name="shipForm">
Not Valid, the `action' attribute is missing.
<input type="text" name="number" size="5">
type="text" is the default for `input' elements and can be safely omitted.
<input type="radio" name="package"
onClick="Package(shipForm)">Pallet
<input type="radio" name="package"
onClick="Package(shipForm)">Box
<input type="radio" name="package"
onClick="Package(shipForm)">Envelope
<input type="radio" name="package"
onClick="Package(shipForm)">Other
But then the `click' event bubbles by default and so could be better handled
at the parent `form' element:

function _package(f)
{
var o;
if (o = f.elements
&& (o = o['otherPackage']))
{
var b = f.elements['package'][3].checked;
if (!b)
{
o.value = "";
}

o.style.backgroundColor = b ? '#fff' : '#ccc';
o.style.color = b ? '#000' : '#333';
o.disabled = b;

if (b && isMethodType(typeof o.focus))
{
o.focus();
}
}
}

<form ... onclick="_package(this);">
<input type="radio" name="package" id="pkg_pallet" value="pallet"<label for="pkg_pallet">Pallet</label><br> <input type="radio" name="package" id="pkg_box" value="box"<label for="pkg_box">Box</label><br> <input type="radio" name="package" id="pkg_envelope" value="envelope"<label for="pkg_envelope">Envelope</label><br> <input type="radio" name="package" id="pkg_other" value="other"<label for="pkg_other">Other</label><br> ...
</form>
<input type="text" name="otherPackage"
disabled style="background-color: #DDDDDD">


That color is not Truly Web-Safe[tm], and the foreground color should
be declared, too. See above.

Furthermore, the element should not be disabled by default but in the
`onload' event handler, because otherwise it becomes useless to users
without client-side script support:

<body onload="document.forms['shipForm'].elements['otherPackage'].disabled
= true;">
...
<form action="..." name="shipForm">
...
<input name="otherPackage" style="background-color:#ccc; color:#666">
...
</form>
...
</body>
PointedEars
Dec 30 '05 #5
Thomas 'PointedEars' Lahn said the following on 12/30/2005 2:46 AM:
McKirahan wrote:

<html>
<head>
<title>Package.htm</title>

Not Valid. <URL:http://validator.w3.org/>


Irrelevant to the thread.
<script type="text/javascript">
function Package(thisform) {

Identifiers of methods not intended to be used as constructors should not
start with a capital letter, not even to work around a reserved word issue.


Irrelevant to the thread.
function _package(inp)
{

if (thisform.package[3].checked) {

Should be

if (thisform.form.elements['package'][3].checked)


If thisform is a reference to the form itself then the reference back to
its form is redundant as you are already at the form level.
aso. `thisform' is a reference to the HTMLFormElement, but that should
not be passed with `shipForm' (it's IE-proprietary); instead, `this' should
be passed and the `form' property of the referenced event target object
should be used. Form controls should be referenced through the `elements'
collection when possible (as here).

thisform.otherPackage.style.backgroundColor = "#FFFFFF";

<URL:http://www.w3.org/QA/Tips/color>


Irrelevant to the thread.
thisform.otherPackage.disabled = false;
thisform.otherPackage.focus();

<URL:http://pointedears.de/scripts/test/whatami#inference>


Irrelevant to the thread.
} else {
thisform.otherPackage.value = "";
thisform.otherPackage.style.backgroundColor = "#DDDDDD";
thisform.otherPackage.disabled = true;

Since `thisform.otherPackage' is used very often, it is prudent to assign
the reference to a variable and use the latter in place once it has been
established that it stores a valid reference:


Irrelevant to the thread.
var o = thisform.otherPackage;
if (o)
{
o.... = ...
}

or, if the recommendation above is followed:

var f, o;
if (inp
&& (f = inp.form)
&& (o = f.elements)
&& (o = o['otherPackage']))
{
var b = f.elements['package'][3].checked;
if (!b)
{
o.value = "";
}

o.style.backgroundColor = b ? '#fff' : '#ccc';
o.style.color = b ? '#000' : '#333';
o.disabled = b;

if (b && isMethodType(typeof o.focus))
{
o.focus();
}
}

}
}
</script>
</head>
<body>
<form name="shipForm">

Not Valid, the `action' attribute is missing.


Irrelevant to the thread.
<input type="text" name="number" size="5">

type="text" is the default for `input' elements and can be safely omitted.


Irrelevant to the thread.
<input type="radio" name="package"
onClick="Package(shipForm)">Pallet
<input type="radio" name="package"
onClick="Package(shipForm)">Box
<input type="radio" name="package"
onClick="Package(shipForm)">Envelope
<input type="radio" name="package"
onClick="Package(shipForm)">Other

But then the `click' event bubbles by default and so could be better handled
at the parent `form' element:


Irrelevant to the thread.
function _package(f)
{
var o;
if (o = f.elements
&& (o = o['otherPackage']))
{
var b = f.elements['package'][3].checked;
if (!b)
{
o.value = "";
}

o.style.backgroundColor = b ? '#fff' : '#ccc';
o.style.color = b ? '#000' : '#333';
o.disabled = b;

if (b && isMethodType(typeof o.focus))
{
o.focus();
}
}
}

<form ... onclick="_package(this);">
<input type="radio" name="package" id="pkg_pallet" value="pallet"
><label for="pkg_pallet">Pallet</label><br> <input type="radio" name="package" id="pkg_box" value="box"
><label for="pkg_box">Box</label><br>

<input type="radio" name="package" id="pkg_envelope" value="envelope"
><label for="pkg_envelope">Envelope</label><br>

<input type="radio" name="package" id="pkg_other" value="other"
><label for="pkg_other">Other</label><br>

...
</form>
<input type="text" name="otherPackage"
disabled style="background-color: #DDDDDD">

That color is not Truly Web-Safe[tm], and the foreground color should
be declared, too. See above.


Irrelevant to the thread.
Furthermore, the element should not be disabled by default but in the
`onload' event handler, because otherwise it becomes useless to users
without client-side script support:

<body onload="document.forms['shipForm'].elements['otherPackage'].disabled
= true;">
use window.onload, not <body onload
...
<form action="..." name="shipForm">
...
<input name="otherPackage" style="background-color:#ccc; color:#666">
...
</form>
...
</body>
PointedEars

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Answer:It destroys the order of the conversation
Dec 30 '05 #6
On 30/12/2005 12:44, Randy Webb wrote:

[snip]
Irrelevant to the thread.
And you've never posted something irrelevant to a thread? What about
others that have? I know you don't like Thomas, but come on, play nice.
-- [...] Answer:It destroys the order of the conversation


Didn't you forget the question?

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Dec 30 '05 #7
Thomas 'PointedEars' Lahn <Po*********@web.de> wrote in
news:46****************@PointedEars.de:
McKirahan wrote:
<html>
<head>
<title>Package.htm</title>
Not Valid. <URL:http://validator.w3.org/>


Really?

The content for the TITLE element is parsed character data (#PCDATA), and
apparently can even include character references (character entity and
numeric character references). The HTML 4.01 specification refers the
reader to the ISO 8879 standard for a definitive reading of #PCDATA.

Cutting and pasting this HTML into the W3C validator and setting Doctype
for 4.01 Transitional, there was only one error making it invalid, which
was the missing 'action' attribute for the form element.

You must have caught the validator when it had a fever that day. ;-)
[excised rest of message]

PointedEars


Dec 30 '05 #8
Patient Guy wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> wrote [...]:
McKirahan wrote:
<html>
<head>
<title>Package.htm</title> Not Valid. <URL:http://validator.w3.org/>


Really?


Really.
The content for the TITLE element is parsed character data (#PCDATA), and
apparently can even include character references (character entity and
numeric character references). [...]
I was not referring to the `title' element, but to all of the quoted code up
to this line which lacks the DOCTYPE declaration and lacks the character
set declaration for non-HTTP service. The snippet claims to be a HTML
document; it is not.
Cutting and pasting this HTML into the W3C validator and setting Doctype ^^^^^^^^^^^^^^^ for 4.01 Transitional, ^^^^^^^^^^^^^^^^^^^^^
See?
there was only one error making it invalid, which was the missing 'action'
attribute for the form element.


You have not validated the document through file upload which would have
revealed the missing character set declaration, too.
PointedEars
Dec 30 '05 #9
Michael Winter said the following on 12/30/2005 10:34 AM:
On 30/12/2005 12:44, Randy Webb wrote:

[snip]
Irrelevant to the thread.

And you've never posted something irrelevant to a thread? What about
others that have? I know you don't like Thomas, but come on, play nice.


Yes sir :)
--


[...]
Answer:It destroys the order of the conversation

Didn't you forget the question?


Or forgot to snip it all :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 30 '05 #10

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

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
11
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
2
by: Aaron Ackerman | last post by:
I cannot a row to this bound DataGrid to SAVE MY LIFE! I have tried everything and I am at a loss. The using goes into add mode with the add button adds his data then updates with the update...
16
by: Ajay | last post by:
Hi all, i want to know when i create a class.what all it contains.I know the following things are there by default if i do not declare them by myself.Please tell the other things that are left. ...
8
by: watkinsdev | last post by:
Hi, I have created a mesh class in visual studio 6.0 c++. I can create a device, render objects and can edit the objects by for instancnce selecting a cluster of vertices and processing the...
0
by: shapper | last post by:
Hello, I am creating a class with a control. I compiled the class and used it on an Asp.Net 2.0 web site page. I can see the begin and end tags of my control (<oland </ol>) but somehow the...
10
by: DavidSeck.com | last post by:
Hi, I am working with the Facebook API right now, an I have kind of a problem, but I don't know what I am doing wrong. So I have a few arrays, f.ex.: User albums: array(2) {
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.