Qn from Novice

P

peter

(i'm new to javascript) I found the following Javascript and is confused by
its syntax. Why the expression "window.status=input.form[0].value;" is
valid (i verified this in ie6) and has the same result as the expression
"window.status=input.form.elements[0].value"?

As fas as I understand, "input.form" represents the form object. And, this
form object isnt an array. Why we can use array accessing syntax on an
ordinary object?

Any expert can help? thx
============================================================================
=====

<html><HEAD>
<!-- **************************************************************** -->
<SCRIPT LANGUAGE="JavaScript">

function displayFormValue(input) {
window.status=input.form[0].value;
// why this syntax can be used instead of "input.form.elements[0].value"
return true;
}
</script>
<!-- **************************************************************** -->
</HEAD><body>
<form name=form1>
<input name=text1 type=text onKeyUp="return displayFormValue(this);"
size=10/>

</form>
<br>good to see you
</body>
</html>
 
V

VK

There is not such array as form(), but there is forms() array:
document.forms.elements[j]

displayFormValue(this) gives you a reference on the input element itself, so
in your function you just say:
window.status=input.value;
which is the right syntacs

Any of other syntacs work in IE only because this browser has been built
with a very high tolerance to bad syntacs.
As long as IE can get any rough idea what in the name are trying to do, it
will re-adjust your code internally in some more descent way, and it will
execute the result.
Sometimes it's good, sometimes it's bad (first of all, no guarantee that IE
will decrypt your intentions properly; secondly, it may produce well-hidden
sporadic bugs).
 
M

Martin Honnen

peter said:
(i'm new to javascript) I found the following Javascript and is confused by
its syntax. Why the expression "window.status=input.form[0].value;" is
valid (i verified this in ie6) and has the same result as the expression
"window.status=input.form.elements[0].value"?

As fas as I understand, "input.form" represents the form object. And, this
form object isnt an array. Why we can use array accessing syntax on an
ordinary object?

Well what you think is "array accessing syntax" is simply a way to
access properties of an object in JavaScript, see
http://www.jibbering.com/faq/faq_notes/square_brackets.html
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top