By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,361 Members | 3,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,361 IT Pros & Developers. It's quick & easy.

getElementById finds an element in IE but not Firefox

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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>

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2

P: n/a
Dave Hammond wrote:
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.


The getElementById (as its name implies) finds an element with a matching
ID attribute. IE on windows will also find elements with a matching NAME
attribute, but this is non-standard.
Jul 23 '05 #3

P: n/a
Dave Hammond wrote:
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
Jul 23 '05 #4

P: n/a
I changed the NAME= to ID= and everything works as expected.

Thanks for the quick response!

-Dave H.

Jul 23 '05 #5

P: n/a
JRS: In article <Xn*******************@194.109.133.29>, dated Tue, 15
Feb 2005 13:35:52, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> posted :

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()

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6

P: n/a
>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>

Jul 23 '05 #7

P: n/a
Robert wrote:
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>


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)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #8

P: n/a
Randy Webb wrote:
Robert wrote:
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>


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)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #9

P: n/a
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".

--
Fred
Jul 23 '05 #10

P: n/a
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>

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.