how to refer correctly to innerHTML?

L

lcplben

Hello everyone --

Probem page is at:

www.sellmycalls.com/str-final-char.html

I don't see the errors in this script, which tries to toggle a <th>
element through three states.

The var "final" is supposed to receive the last character in the
innerHTML of the incoming element. Based upon that character, I want
to replace that character in the element; to change the background
color of the element; and to change the title of the element.

First, I haven't been able to make IE7 understand what I mean by
e.innerHTML[e.innerHTML.length-1].

THen, I haven't communicated to any of IE7, FF3.5.3, or Safari any of
the things I want to do, except that the background color changes.

What IE need am I ignoring; and what else am I doing wrong in this
script?

Thanks!

-- ben
 
G

Garrett Smith

lcplben said:
Hello everyone --

Probem page is at:

www.sellmycalls.com/str-final-char.html

I don't see the errors in this script, which tries to toggle a <th>
element through three states.

The var "final" is

Final is a Reserved Word.

supposed to receive the last character in the
innerHTML of the incoming element. Based upon that character, I want
to replace that character in the element; to change the background
color of the element; and to change the title of the element.
A character cannot be replaced. innerHTML can be set.

CharacterData can, in some browsers, be modified.
http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-FF21A306

Not recommended for cross browser compatibility.

If the toggle char in its own element, the element can be styled
(position, color, etc) independent of the text.
First, I haven't been able to make IE7 understand what I mean by
e.innerHTML[e.innerHTML.length-1].

I'd expect the result to be undefined in IE and a character in
Firefox/Safari (which have a syntax extension for character access
of string literal).

Rather than trying to "make IE understand you", you'll have much better
success in trying to understand browsers and relevant standards.
 
T

Thomas 'PointedEars' Lahn

lcplben said:
[...]
First, I haven't been able to make IE7 understand what I mean by
e.innerHTML[e.innerHTML.length-1].

That was to be expected. `e.innerHTML', where supported, stores a string
value. For some reason you appear to expect an ECMAScript string value to
be an array of characters; it is not.

As a proprietary extension, though, Netscape/Mozilla.org JavaScript (and
maybe other implementations) provides access to the characters of a string
as if it was an array of characters, meaning that the property access `s'
works there as if the expression `s.charAt(i)' was used.

However, IE/MSHTML supports JScript instead, so you need the latter fully
standards-compliant approach there. And for compatibility you should use it
everywhere.
THen, I haven't communicated to any of IE7, FF3.5.3, or Safari any of
the things I want to do, except that the background color changes.

What IE need am I ignoring; and what else am I doing wrong in this
script?

RTFM instead of copy-and-pray would help, and debugging instead of guessing
would tell.

<http://jibbering.com/faq/#posting>


PointedEars
 
V

VK

Thomas said:
That was to be expected.  `e.innerHTML', where supported, stores a string
value.  For some reason you appear to expect an ECMAScript string valueto
be an array of characters

Why would be the opposite expected if all known browsers from Netscape
4.x and to the most recent Chrome are treating the string in exactly
this way? The only exception on the market is IE and if OP just
started to adjust his script for this highly particular platform then
his surprise is well understood.
; it is not.

Alas true. To make it work for IE as well a convoluted way is needed
using charAt method. The whole approach is not something I would do,
but if you want to make *your* code to work rather than using my, then

function toggleSort ( elm ) {
var html = elm.innerHTML;
var L = html.length-1;
var sign = elm.innerHTML.charAt(L);
var name = elm.innerHTML.substring(0, L);
if (sign == '-') {
elm.innerHTML = name + '+';
elm.style.backgroundColor = '';
}
else {
elm.innerHTML = name + '-';
elm.style.backgroundColor = 'yellow';
}
return false;
}
 
T

Thomas 'PointedEars' Lahn

VK said:
Why would be the opposite expected if all known browsers from Netscape
4.x and to the most recent Chrome are treating the string in exactly
this way?

Non sequitur. They don't.


PointedEars
 
V

VK

Non sequitur.  They don't.

Netscape 4.79 Win98 - does
Firefox 3.5.3 Vista - does
Safari 4.0 Vista - does
Chrome 3.0 Vista - does
Opera 10.0 Vista - does

IE6 WinXP SP2 - doesn't
IE8 Vista - doesn't

Surely it can be N amount of different "jamberries" and other
fantastic devices with the most fantastic features up to exploding
right to one's face on attempt to access the innerHTML property. But
the serious browser market is firm in the spelled approach with IE
remaining the only ugly exception.
 
T

Thomas 'PointedEars' Lahn

VK said:
Netscape 4.79 Win98 - does
Firefox 3.5.3 Vista - does
Safari 4.0 Vista - does
Chrome 3.0 Vista - does
Opera 10.0 Vista - does

IE6 WinXP SP2 - doesn't
IE8 Vista - doesn't

In *none* of the mentioned browsers is a string value treated as an array of
characters.


PointedEars
 
R

rf

VK said:
Netscape 4.79 Win98 - does
Firefox 3.5.3 Vista - does
Safari 4.0 Vista - does
Chrome 3.0 Vista - does
Opera 10.0 Vista - does

Wrong.

If a string were treated exactly as an array of characters then this would
work:

var a = "abcde";
a[1] = 'w';

Show us a browser where it does. Or, STFU.
 
L

lcplben

Final is a Reserved Word.
Thanks.

A character cannot be replaced. innerHTML can be set.

Aha! Thanks, did not know.
I'd expect the result to be undefined in IE and a character in
Firefox/Safari (which have a syntax extension for character access
of string literal).

Yes, I see that now.
Rather than trying to "make IE understand you", you'll have much better
success in trying to understand browsers and relevant standards.

Well understood.

-- ben
 

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,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top