Determine Input Box Property

C

crjunk

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
 
I

Ivo

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
 
C

crjunk

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
 
G

Gérard Talbot

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) {
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-developer/upgrade_2.html#dom_access

Gérard
 
R

Randy Webb

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 - []. So, the original code ...["txt...]. was correct. Using ()
for form elements is an IE only way of accessing them.
 
J

Jambalaya

<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.
 
R

Richard Cornford

Randy said:
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.
 
R

Richard Cornford

Richard Cornford wrote:
... . 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.
 
C

crjunk

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top