HighDots Forums  

getElementById finds an element in IE but not Firefox

Javascript JavaScript language (comp.lang.javascript)


Discuss getElementById finds an element in IE but not Firefox in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dave Hammond
 
Posts: n/a

Default getElementById finds an element in IE but not Firefox - 02-15-2005 , 08:22 AM






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.


Reply With Quote
  #2  
Old   
Evertjan.
 
Posts: n/a

Default Re: getElementById finds an element in IE but not Firefox - 02-15-2005 , 08:35 AM






Dave Hammond wrote on 15 feb 2005 in comp.lang.javascript:

Quote:
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)



Reply With Quote
  #3  
Old   
Duncan Booth
 
Posts: n/a

Default Re: getElementById finds an element in IE but not Firefox - 02-15-2005 , 08:36 AM



Dave Hammond wrote:

Quote:
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.


Reply With Quote
  #4  
Old   
Dietmar Meier
 
Posts: n/a

Default Re: getElementById finds an element in IE but not Firefox - 02-15-2005 , 08:37 AM



Dave Hammond wrote:

Quote:
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


Reply With Quote
  #5  
Old   
Dave Hammond
 
Posts: n/a

Default Thanks all! - 02-15-2005 , 09:11 AM



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

Thanks for the quick response!

-Dave H.


Reply With Quote
  #6  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: getElementById finds an element in IE but not Firefox - 02-15-2005 , 05:52 PM



JRS: In article <Xns95FE94839C30eejj99 (AT) 194 (DOT) 109.133.29>, dated Tue, 15
Feb 2005 13:35:52, seen in news:comp.lang.javascript, Evertjan.
<exjxw.hannivoort (AT) interxnl (DOT) net> posted :
Quote:
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.


Reply With Quote
  #7  
Old   
Robert
 
Posts: n/a

Default Re: getElementById finds an element in IE but not Firefox - 02-15-2005 , 06:54 PM



Quote:
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>



Reply With Quote
  #8  
Old   
Randy Webb
 
Posts: n/a

Default Re: getElementById finds an element in IE but not Firefox - 02-15-2005 , 07:22 PM



Robert wrote:
Quote:
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


Reply With Quote
  #9  
Old   
Randy Webb
 
Posts: n/a

Default Re: getElementById finds an element in IE but not Firefox - 02-15-2005 , 07:33 PM



Randy Webb wrote:

Quote:
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


Reply With Quote
  #10  
Old   
Fred Oz
 
Posts: n/a

Default Re: getElementById finds an element in IE but not Firefox - 02-15-2005 , 08:00 PM



Dietmar Meier wrote:
[...]
Quote:
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


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.