getElementById finds an element in IE but not Firefox

D

Dave Hammond

Hi All,

The following code works in IE but not Firefox. IE produces the
expected "this is more text!" output, but Firefox produces "no more
text". Any ideas why?

<BODY>
<FORM>
<INPUT TYPE=HIDDEN NAME="moreText" VALUE="<PRE>This is more
text!</PRE>">
<SCRIPT LANGUAGE="javascript">
var m = (document.getElementById &&
document.getElementById('moreText'))
? document.getElementById('moreText').value
: '<PRE>no more text</PRE>';
document.writeln(m);
</SCRIPT>
</FORM>
</BODY>

FYI, I've tried it with the script both within and outside the form,
and the code behaves identically.

Thanks!
-Dave H.
 
E

Evertjan.

Dave Hammond wrote on 15 feb 2005 in comp.lang.javascript:
<SCRIPT LANGUAGE="javascript">
var m = (document.getElementById &&
document.getElementById('moreText'))
? document.getElementById('moreText').value
: '<PRE>no more text</PRE>';
document.writeln(m);
</SCRIPT>

You cannot do a document.write() when the page is already loaded and on
screen without destroying the whole page, including the javascript.

Try DOM-inserting (innerHTML or child) to a <div>
 
D

Dietmar Meier

Dave said:
The following code works in IE but not Firefox.
[...]
<INPUT [...] NAME="moreText" [...]> ~~~~
[...]
document.getElementById('moreText')
~~~~
[...]

In MSIE only, name and id attributes share one namespace.
Simply add an id="moreText" attribute to the input element.

ciao, dhgm
 
D

Dave Hammond

I changed the NAME= to ID= and everything works as expected.

Thanks for the quick response!

-Dave H.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 15
Feb 2005 13:35:52, seen in Evertjan.
You cannot do a document.write() when the page is already loaded and on
screen without destroying the whole page, including the javascript.


But
for (j=0 ; j<10 ; j++) document.write(j)

shows on the new page 0123456789 so the script is not immediately
destroyed by the first document.write(). Or, if it is immediately
destroyed on other browsers, I need to do some site re-design.

The original page is immediately destroyed, so all input controls needed
must be read before the first such document.write()
 
R

Robert

You cannot do a document.write() when the page is already loaded and
on
screen without destroying the whole page, including the javascript.

This is a true statement, but it doesn't apply to the original poster
becuase the page hasn't been loaded since the </body> hasn't been
encountered.


I added the id tag to make it work in more browsers.

<BODY>
<FORM>
<INPUT TYPE=HIDDEN NAME="moreText" id="moreText" VALUE="<PRE>This is
more
text!</PRE>">
<SCRIPT LANGUAGE="javascript">
var m = (document.getElementById &&
document.getElementById('moreText'))
? document.getElementById('moreText').value
: '<PRE>no more text</PRE>';
document.writeln(m);
</SCRIPT>
</FORM>
</BODY>
 
R

Randy Webb

Robert said:
This is a true statement, but it doesn't apply to the original poster
becuase the page hasn't been loaded since the </body> hasn't been
encountered.


I added the id tag to make it work in more browsers.

<BODY>
<FORM>
<INPUT TYPE=HIDDEN NAME="moreText" id="moreText" VALUE="<PRE>This is
more
text!</PRE>">
<SCRIPT LANGUAGE="javascript">
var m = (document.getElementById &&
document.getElementById('moreText'))
? document.getElementById('moreText').value
: '<PRE>no more text</PRE>';
document.writeln(m);
</SCRIPT>
</FORM>
</BODY>

You could make it work in even more browsers if you used the forms
collection and referenced the form element by its name attribute. If for
no other reason that there are more browsers that support the forms
collection than browsers that support document.getElementById

var m = (document.forms &&
document.forms['moreText'] &&
document.forms['moreText'].value)
? document.forms['moreText'].value
: '<pre>No More Text</pre>';
document.write(m)
 
R

Randy Webb

Randy said:
Robert said:
This is a true statement, but it doesn't apply to the original poster
becuase the page hasn't been loaded since the </body> hasn't been
encountered.


I added the id tag to make it work in more browsers.

<BODY>
<FORM>
<INPUT TYPE=HIDDEN NAME="moreText" id="moreText" VALUE="<PRE>This is
more
text!</PRE>">
<SCRIPT LANGUAGE="javascript">
var m = (document.getElementById &&
document.getElementById('moreText'))
? document.getElementById('moreText').value
: '<PRE>no more text</PRE>';
document.writeln(m);
</SCRIPT>
</FORM>
</BODY>

You could make it work in even more browsers if you used the forms
collection and referenced the form element by its name attribute. If for
no other reason that there are more browsers that support the forms
collection than browsers that support document.getElementById

var m = (document.forms &&
document.forms['moreText'] &&
document.forms['moreText'].value)
? document.forms['moreText'].value
: '<pre>No More Text</pre>';
document.write(m)

My fingers get ahead of my brain sometimes:

var m = (document.forms &&
document.forms['formName'] &&
document.forms['formName'].elements['moreText'] &&
document.forms['formName'].elements['moreText'].value)
? document.forms['formName'].elements['moreText'].value
: '<pre>No More Text</pre>';
document.write(m)
 
F

Fred Oz

Dietmar Meier wrote:
[...]
In MSIE only, name and id attributes share one namespace.
Simply add an id="moreText" attribute to the input element.

Possibly a tad *too* concise Dietmar ;-)

I'm certain the following is what was intended:

[That method works in]... MSIE only, [the W3C HTML specification
states that] name and id attributes share one namespace.

Which the IE developers seem to have interpreted as "let's treat
ID and NAME as if they are the same thing - but not always".
 
E

Evertjan.

Dr John Stockton wrote on 15 feb 2005 in comp.lang.javascript:
The original page is immediately destroyed, so all input controls
needed must be read before the first such document.write()

Let's try [IE6]:

<html>
<body onload="c()">
<input id=b value='hello there'>
</body>
</html>

<script type='text/javascript'>
function c(){
a=document.getElementById('b').value

document.open()
alert('1 '+a)
document.write('<br>'+a);
alert('2: '+a)
z=document.getElementById('b').value
// here the script seems to be gone, no error however !!!
document.write('<br>'+z);
alert('3: '+z)
}
</script>
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top