Very simple question from a novice

  • Thread starter Knut Olsen-Solberg
  • Start date
V

VK

Knut said:
Thanks!
Am I right assuming that getElementById('aID') cannot be used if aID is something in a form, e.g. an input, or an image?
I have tried, and if we have <INPUT id="ainp" type="text" size=9"> then
a=parseFloat(document.getElementById('ainp').innerHTML); does not work.
Nor does
a=parseFloat(document.getElementById('ainp').value);

I can make this work though: <INPUT name="ainp" type="text" size=9"> // name, not id
a=parseFloat(document.aform.ainp.value); // Of course the form had the name="aform".

I would highly appreciate to see these rules. I have searched for information about this for several hours, but they are either buried in an awful lot of details, or I just get small pieces of it...

You may play with this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html401/loose.dtd">
<html>
<head>
<title>JavaScript</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">

<script type="text/javascript">
function swap() {
var tmp = $('para1').innerHTML;
$('para1').innerHTML = $('para2').innerHTML;
$('para2').innerHTML = tmp;

tmp = document.forms['myForm'].elements['text1'].value;
document.forms['myForm'].elements['text1'].value
= document.forms['myForm'].elements['text2'].value;
document.forms['myForm'].elements['text2'].value = tmp;
}

function $(id) {
return document.getElementById(id);
}

</script>
</head>

<body>
<p id="para1">This is first line</p>
<p id="para2">Here is second line</p>
<form name="myForm" action="" method="post">
<input type="text" name="text1" value="This is first value"><br>
<input type="text" name="text2" value="This is second value"><br>
<input type="button" value="Swap" onClick="swap()">
</form>
</body>
</html>


In the first swap block (getElementById) you are using DOM 1 methods.


In the second swap block (document.forms) you are using form-specific
methods from DOM 0.

You can use DOM 1 instead of DOM 0. Thus you can give ID's to form
elements and retrieve getElementById('ElementID').value.

You cannot use DOM 0 instead of DOM 1 because DOM 0 simply doesn't have
an ability to address any given element on the page.

In any case .value property appertains to form elements. DOM elements
like <p> <div> etc. do have innerHTML and nodeValue properties instead.
 
J

Jim Ley

btw what about the FAQ posting problem?
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/2b234f5ba2cae095>

I'm not looking for glory or cratitude of any kind :) but at least it
could be *some* answer like "we changed our mind, screw you and your
program!"

I had to go to Holland for meetings, then I went to map the Isle of
Wight with the openstreetmap.org people, the FAQ restarting got lost
in a 3 hour delay at schiphol when I was sitting on the tarmac.

It will happen, and after looking at your code, there will be changes
- I ain't running it under a webserver, but it will be substantially
based on your code I'm sure. An annoucement will be made when it's
back

Cheers,

Jim.
 
R

Randy Webb

Dr John Stockton said the following on 5/8/2006 10:23 AM:
JRS: In article <[email protected]>, dated
Sun, 7 May 2006 15:00:08 remote, seen in

You lack the ability to judge.

I have never said differently. But coming from someone that is as
judgmental as you are, that is hilarious.
And being a typical Merkin, you resent being given the treatment that
you feebly apply to others.

Your ignorance is overwhelming. The only person that is more judgmental,
arrogant, and condescending in this group than you is TL and you are
approaching him very quickly.

Again, I thought you were more intelligent than that but I was wrong.

If you want to continue the personal attacks, then go ahead. I won't
stoop to your level.
You have failed to perceive the distinction.

No, you have failed to understand that I do know the distinction. The
code in the FAQ is *NOT* used for being "briefer" or "clearer", it is
used for *speed*. And no other reason. As I said, that argument was
hashed to death - search the archives.
A call to Wryt is considerably shorter that what would otherwise be used,
and less susceptible to typos; but it's hard to see how one could code
anything more briefly than +.

Wryt is just as susceptible to typos as anything else. And for someone
looking in the FAQ to learn from, not letting them learn the mistakes of
typos is doing them a disservice.
I referred to "such an innerHTML assignment"; you are not justified
in extending the reference in that manner.

The comparison is valid. You said that the speed difference was
"unlikely to be significant" and I applied that same reasoning to your
own code. You didn't like it though.
In fact, you would be unjustified in extending it to LZ, although there a
corresponding argument is applicable. While it's not necessarily
obvious that the unary + operator is allowed, there's only two possible
meanings only one of which is reasonable.

Only two uses for + ? Wrong.
That's not what I wrote, though.

It doesn't matter what is written, what matters is the interpretation
and you implied precisely what I wrote.
If you actually read what I wrote in the earlier quote, even you should
be able to see that it was *telling* the reader that he should not have
duplicates (within the script) for the names used as the first argument
of Wryt. For an absolute prohibition, "must" would have been used.

And you think that readers of the FAQ who are trying to learn something
from it are going to search for this thread and understand that? Heck,
they can't even find the FAQ and understand what the unary + does, much
less know to try to find this thread. Your pedantics get boring.
 
V

VK

Jim said:
after looking at your code, there will be changes
- I ain't running it under a webserver, but it will be substantially
based on your code I'm sure. An annoucement will be made when it's
back

Thanks, ACK
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Tue, 9 May 2006 00:07:36 remote, seen in
Randy Webb said:
Dr John Stockton said the following on 5/8/2006 10:23 AM:

On the fifth of August? I think not. And the time was 15:23.

Wryt is just as susceptible to typos as anything else. And for someone
looking in the FAQ to learn from, not letting them learn the mistakes of
typos is doing them a disservice.

Using Wryt is less susceptible. In each case, one must type the ID &
display string correctly, and a pair of parentheses. A typo in "Wryt"
will generally give an error message on first test. But a type in
"innerHTML" will result in the assignment of the string to a new
property without affecting the screen. That's less noticeable.

Only two uses for + ? Wrong.

Only two *possible* meanings for the *unary* + *operator* is what I
wrote. Binary + either concatenates strings (one of which may have been
converted from a Number) or adds Numbers. Therefore, the only possible
effects for unary + are conversion to String and conversion to Number.

Perhaps if you were to read a larger proportion of the words in each
sentence you would get better results.


One understands that the USA claims to lead the World. One observes
that the trend in educational standards, over the last forty years, is
dumbing down. The observed effect is therefore as should be expected.
 
R

[Rodrigo Souza] NUTS

function swap() {
document.getElementById('para1').innerHTML="New text";
}
 
R

Randy Webb

Dr John Stockton said the following on 5/9/2006 2:30 PM:
JRS: In article <[email protected]>, dated
Tue, 9 May 2006 00:07:36 remote, seen in

On the fifth of August? I think not. And the time was 15:23.

I did not write the fifth of August. I have explained it to you before,
and evidently you didn't understand it. But if you are ignorant enough
to think in May that I am referring to a date in August then you are
worse than I thought.

But try harder son.
Using Wryt is less susceptible. In each case, one must type the ID &
display string correctly, and a pair of parentheses. A typo in "Wryt"
will generally give an error message on first test. But a type in
"innerHTML" will result in the assignment of the string to a new
property without affecting the screen. That's less noticeable.

You need to go to your Physician and get a prescription for Prozac. You
don't think that not changing the visible display wouldn't be
noticeable? You really are a work of art my boy.
Only two *possible* meanings for the *unary* + *operator* is what I
wrote. Binary + either concatenates strings (one of which may have been
converted from a Number) or adds Numbers. Therefore, the only possible
effects for unary + are conversion to String and conversion to Number.

Again, and you think that people reading the FAQ attempting to learn
will know the difference between the different uses of +?

<snipped some more of JRS' pedantic ramblings about the USA>

Irony: If it weren't for the US, JRS wouldn't even be posting here.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 9 May
2006 23:52:08 remote, seen in Randy Webb
Dr John Stockton said the following on 5/9/2006 2:30 PM:

You need to go to your Physician and get a prescription for Prozac. You
don't think that not changing the visible display wouldn't be
noticeable? You really are a work of art my boy.

But I did not write that it would not be noticeable, but that it would
be less noticeable. You're still not reading what you respond to with
sufficient care or ability.

Again, and you think that people reading the FAQ attempting to learn
will know the difference between the different uses of +?

Irrelevant. I'm there refuting your error in misinterpreting my
previous statement. Don't quote what you have not read.
 
R

Randy Webb

Dr John Stockton said the following on 5/10/2006 4:59 PM:
JRS: In article <[email protected]>, dated Tue, 9 May
2006 23:52:08 remote, seen in Randy Webb


But I did not write that it would not be noticeable, but that it would
be less noticeable. You're still not reading what you respond to with
sufficient care or ability.

You really are as ignorant as you act at times.

If nothing happens on the page, at all, because the user has error
messages disabled (IE) or they are using Firefox and don't know where
the Error Console is, then they see the same thing (with the exception
of the yellow ! in IE if they know to look for it ) - nothing. Nothing
is nothing. And one is not "less noticeable" than the other. They are
both the same.

Your pedantic juvenile behavior gets old.

But keep trying my son, you can do it.
Irrelevant.

Only from you. You make a statement, I apply your logic to another
scenario that is similar, and you reply "Irrelevant".
I'm there refuting your error in misinterpreting my previous statement.

The only problem with that is you don't understand what I wrote, nor can
you properly interpret it based on your previous behavior. You are
starting to remind me of TL.
 
T

Thomas 'PointedEars' Lahn

VK said:
You may play with this:

Or rather not.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html401/loose.dtd">

http://www.w3.org/TR/html4/loose.dtd

is the recommended URI, pointing to the latest HTML version.
<html>
<head>
<title>JavaScript</title>

<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">

Use this only if you really know what you are doing. It is recommended
only for resources that are not served via HTTP. Because it is potentially
harmful for resources that are served via HTTP: the HTTP Content-Type
header takes precedence over this, and if both values are different, the
result is most certainly not what you would expect (without knowing this).
<script type="text/javascript">
function swap() {
var tmp = $('para1').innerHTML;
$('para1').innerHTML = $('para2').innerHTML;
$('para2').innerHTML = tmp;

The return value of $() should be tested before it is used with
property accessors, to avoid runtime errors.

`innerHTML' is a proprietary property that should be tested before
it is read or written to. Standards compliant access methods should
be preferred, mixing standards compliant and proprietary features
should be avoided.

tmp = document.forms['myForm'].elements['text1'].value;
document.forms['myForm'].elements['text1'].value
= document.forms['myForm'].elements['text2'].value;
document.forms['myForm'].elements['text2'].value = tmp;

One wants to optimize here:

var
es = document.forms['myForm'].elements,
text1 = es['text1'],
text2 = es['text2'];

tmp = text1.value;
text1.value = text2.value;
text2.value = tmp;

Further feature tests might be appropriate.
}

function $(id) {
return document.getElementById(id);
}

"The dollar sign is intended for use only in mechanically generated code."
(ECMAScript Language Specification, Edition 3 Final, 7.6 Identifiers)

And the method as is, allowing a shorter identifier aside, is entirely
redundant. It only adds up to stack memory usage.
</script>
</head>

<body>
<p id="para1">This is first line</p>
<p id="para2">Here is second line</p>
<form name="myForm" action="" method="post">

This `form' element does not need a name, ...
<input type="text" name="text1" value="This is first value"><br>
<input type="text" name="text2" value="This is second value"><br>

(and type="text" is redundant, that is the default attribute value)
<input type="button" value="Swap" onClick="swap()">

.... if this line is changed to

<input type="button" value="Swap" onclick="swap(this);">

because as per DOM Level 0, each DOM object representing a form control has
a `form' property that refers to the DOM object representing the ancestor
`form' element. And `this' in an intrinsic event handler attribute value
refers to the object that handles the event (provided that the default
`scripting' language is an ECMAScript implementation, which is the case
here); that is the `input' element here.

Furthermore, items that require support of client-side scripting should
be generated by it:

<script type="text/javascript">
document.write(
'<input type="button" value="Swap" onclick="swap(this);">');
</form>
</body>
</html>


In the first swap block (getElementById) you are using DOM 1 methods.

These days it would be rather W3C DOM Level 2, although
Document::getElementById() was already defined in W3C DOM Level 1 indeed.

`innerHTML' as used "in the first swap block" does not qualify as a DOM
Level 0 feature, since neither IE nor Netscape before version 4 supported
it.
In the second swap block (document.forms) you are using form-specific
methods from DOM 0.

True, as this also works with NN3/IE3.
You can use DOM 1 instead of DOM 0. Thus you can give ID's to form
elements and retrieve getElementById('ElementID').value.

Where the latter would be rather inefficient, considering that
the same syntax can be used with IDs for ID-aware user agents.
You cannot use DOM 0 instead of DOM 1

Yes, you can.
because DOM 0 simply doesn't have an ability to address any given element
on the page.

True. Specific elements would be required, such as `input' and `textarea'
elements, and it would be best if they were children of a `form' element
then. However, `textarea' elements can be formatted with CSS so that they
look like a `p' or `div' element; only users with UAs that do not support
CSS (properly) would have to see fixed-font display and things like that.
In any case .value property appertains to form elements. DOM elements
like <p> <div> etc. do have innerHTML and nodeValue properties instead.

There is no such thing as a "DOM element". And a DOM element _object_,
representing a _markup_ element, does not need to have any of these
properties. Neither is the term "DOM" restricted to the W3C DOM, nor
does it imply support of proprietary features like `innerHTML'.


PointedEars
 

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,774
Messages
2,569,600
Members
45,179
Latest member
pkhumanis73
Top