By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

Determine Input Box Property

P: n/a
I have a script that I want to run only when my input box IS NOT
disabled. Can someone tell me if something is wrong with my script? Is
"disabled" the correct property to use?

function TextChanged(i){
if (!document.ScheduleForm["txtGrossPayroll" + i].disabled) {
document.ScheduleForm.txtRecordStatus.value = "Changes Made; Record Not
Saved.";
document.ScheduleForm.txtRecordStatus.style.color = "#FF0000";
}
}
Thanks,
CR Junk

Sep 9 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Ivo
"crjunk" wrote
I have a script that I want to run only when my input box IS NOT
disabled. Can someone tell me if something is wrong with my script? Is
"disabled" the correct property to use?

function TextChanged(i){
if (!document.ScheduleForm["txtGrossPayroll" + i].disabled) {
document.ScheduleForm.txtRecordStatus.value = "Changes Made; Record Not
Saved.";
document.ScheduleForm.txtRecordStatus.style.color = "#FF0000";
}
}


Looks good to me. Not happy with how it runs? Just nitpicking, but the
two lookups into the txtRecordStatus element could be handled more
efficiently by storing it in a temporary variable:

function TextChanged(i){
var sf = document.forms.ScheduleForm.elements;
if ( !sf["txtGrossPayroll" + i].disabled ) {
var rs = sf.txtRecordStatus;
rs.value = "Changes Made; Record Not Saved.";
rs.style.color = "#FF0000";
}
}

hth
ivo


Sep 9 '05 #2

P: n/a
ivo wrote
Just nitpicking, but the two lookups into the txtRecordStatus element could be handled more efficiently by storing it in a temporary variable


Thanks for the information on using a temporary variable.

I found a mistake that I used. Instead of disabled, I have the input
set as readonly - therefore the formula should be :
if (!document.ScheduleForm["txtGr*ossPayroll" + i].readonly)

Does using readonly make any difference?

Thanks,
CR Junk

Sep 9 '05 #3

P: n/a
Ivo
"crjunk" wrote
Instead of disabled, I have the input
set as readonly - therefore the formula should be :
if (!document.ScheduleForm["txtGr*ossPayroll" + i].readonly)

Does using readonly make any difference?


None at all, but note that the DOM properties are case-sensitive and
"readOnly" features a capital O.

A link (watch for wrap):
http://msdn.microsoft.com/workshop/a...erties/readonl
y_1.asp

chrs
ivo

Sep 9 '05 #4

P: n/a
crjunk a écrit :
I have a script that I want to run only when my input box IS NOT
disabled. Can someone tell me if something is wrong with my script? Is
"disabled" the correct property to use?

function TextChanged(i){
if (!document.ScheduleForm["txtGrossPayroll" + i].disabled) {
if (!document.ScheduleForm.elements.namedItem("txtGro ssPayroll +
i).disabled) {
document.ScheduleForm.txtRecordStatus.value = "Changes Made; Record Not
Saved.";
document.ScheduleForm.txtRecordStatus.style.color = "#FF0000";
}
}
Thanks,
CR Junk


Using Web standards ... on accessing for elements:
http://www.mozilla.org/docs/web-deve...tml#dom_access

Gérard
--
remove blah to email me
Sep 10 '05 #5

P: n/a
Gérard Talbot a écrit :
function TextChanged(i){
if (!document.ScheduleForm["txtGrossPayroll" + i].disabled) {


oops

if (!document.ScheduleForm.elements.namedItem("txtGro ssPayroll" +
i).disabled) {

rather

Using Web standards ... on accessing for elements:
http://www.mozilla.org/docs/web-deve...tml#dom_access


Gérard
--
remove blah to email me
Sep 10 '05 #6

P: n/a
Gérard Talbot said the following on 9/9/2005 9:52 PM:
crjunk a écrit :
I have a script that I want to run only when my input box IS NOT
disabled. Can someone tell me if something is wrong with my script? Is
"disabled" the correct property to use?

function TextChanged(i){
if (!document.ScheduleForm["txtGrossPayroll" + i].disabled) {

if (!document.ScheduleForm.elements.namedItem("txtGro ssPayroll +
i).disabled) {


Forms are a collection, and as such, should be accessed via the Array
syntax - []. So, the original code ...["txt...]. was correct. Using ()
for form elements is an IE only way of accessing them.
document.ScheduleForm.txtRecordStatus.value = "Changes Made; Record Not
Saved.";
document.ScheduleForm.txtRecordStatus.style.color = "#FF0000";
}
}
Thanks,
CR Junk


Using Web standards ... on accessing for elements:
http://www.mozilla.org/docs/web-deve...tml#dom_access


--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 10 '05 #7

P: n/a
<snip>Instead of disabled, I have the input set as readonly.
<snip>Does using readonly make any difference?

Yes, the browser should not send the name/value pair for a disabled
form element. It would send it for a read only element.

Sep 11 '05 #8

P: n/a
Randy Webb wrote:
Gérard Talbot said the following on 9/9/2005 9:52 PM:
crjunk a écrit :
I have a script that I want to run only when my input
box IS NOT disabled. Can someone tell me if something
is wrong with my script? Is "disabled" the correct
property to use?

function TextChanged(i){
if (!document.ScheduleForm["txtGrossPayroll" + i].disabled) {

if (!document.ScheduleForm.elements.namedItem(
"txtGrossPayroll + i).disabled) {


Forms are a collection, and as such, should be accessed via
the Array syntax - [].


Well, they should be accessed with property accessors. Bracket notation
is only necessary when the names used do not qualify as Identifiers.
Though bracket notation is recommended for the task regardless of the
names in order to highlight the distinction between names originating
within JS and the DOM and names that originate in mark-up elements, and
to conform with the W3C HTML DOM standard (as explained below).
So, the original code ...["txt...]. was correct.
Functional, yes. Correct is more a matter of opinion. I would quibble
about the use of the 'shortcut' of referencing the form object as a
named property of the document. Because while no HTML DOM has been
identified as not making forms available as properties of the document
we know that some XHTML DOMs only support the W3C HTML DOM standard
method of accessing forms as properties of the - document.forms -
collection, and using the - document.forms - collection works on all
browsers that allow the 'shortcut'. This makes the W3C DOM standard
property accessors the most widely supported and general approach (being
equally viable in all environments that expose forms to scripting, past
present and (theoretically) future).
Using ()
for form elements is an IE only way of accessing them.

<snip>

It is, but that is not relevant here as the function being called is
the - namedItem - method defined as a method of the HTMLCollection
interface in the W3C DOM standard. The problem with the use of this
method is that the older browsers providing pre-W3C HTML DOM access to
forms do not necessarily implement the - namedItem - method. That is not
actually an issue when writing ECMAScritp because the ECMAScript
bindings for the HTMl DOM says:-

<quote cite="http://www.w3.org/TR/2003/
REC-DOM-Level-2-HTML-20030109/ecma-script-binding.html">

Functions of objects that implement the HTMLCollection interface:

item(index)

This function returns an object that implements the Node
interface.

The index parameter is a Number.

Note: This object can also be dereferenced using square
bracket notation (e.g. obj[1]). Dereferencing with an integer
index is equivalent to invoking the item function with that
index.
namedItem(name)

This function returns an object that implements the Node
interface.

The name parameter is a String.

Note: This object can also be dereferenced using square
bracket notation (e.g. obj["foo"]). Dereferencing using a
string index is equivalent to invoking the namedItem function
with that index.
</quote>

And so because the specification requires that the - item - and -
namedItem - methods are effectively mapped to bracket notation property
accessors there is never any need to use the methods directly, and so no
reason to limit cross-browser compatibility through their use. A fully
W3C HTML DOM standard conforming property accessor for the form control
would go:-

document.forms['ScheduleForm'].elements[("txtGrossPayroll + i)]

- and still be fully functional with all of the browsers that have ever
exposed forms to javascript (and, because it is HTML DOM standard, can
reasonably be expected to be equally functional on future HTML DOM
standard browsers).

Richard.
Sep 11 '05 #9

P: n/a
Richard Cornford wrote:
<snip>
... . A fully W3C HTML DOM standard conforming property
accessor for the form control would go:-

document.forms['ScheduleForm'].elements[("txtGrossPayroll + i)]

<snip>

Except for the missing quote mark in the concatenation expression.
Should be:-

document.forms['ScheduleForm'].elements[('txtGrossPayroll' + i)]

Richard.
Sep 11 '05 #10

P: n/a
I determined that part of my problem was that I originally used
disabled when I should have used readOnly. The other mistake that I
made was that I used readonly instead of readOnly. I was not aware
that the property was case-sensitive.

CR Junk

Sep 12 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.