473,237 Members | 1,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 software developers and data experts.

getElementById finds an element in IE but not Firefox

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
10 1861
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
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
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
I changed the NAME= to ID= and everything works as expected.

Thanks for the quick response!

-Dave H.

Jul 23 '05 #5
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
>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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: JJA | last post by:
I'm trying to use document.getElementByID inside a function where the ID is passed as an argument. I get the same error ("Element has no properties") on the same statement inside the commonCheck...
5
by: Christian Ista | last post by:
Hello, I have this javascript function Function MyFunction(v,id){ document.getElementById(id).value = v } No problem with IE but not work with Firefox.
24
by: Robi | last post by:
I have the following problem: I populate a page with a specific amount of <div id="MyTest"> containers inside another <div> container. for (i=0; i < MyString.length; i++) document.write('<div...
3
by: davidkarlsson74 | last post by:
Error: document.getElementById("folderMenu").cells has no properties File: http://www.volkswagen.se/tillbehor/js/foldermenu.js Rad: 49 The function activates different DIV:s, but doesn't seem to...
3
by: palak123i | last post by:
Hi All, I am using a javascript to submit a request using AJAX. Part of javascript code as follows: var favElement = document.getElementById('fav1'); alert(favElement); for (var i = 0; i <...
19
by: vunet.us | last post by:
Hello, My AJAX application paints data into about 500 cells with unique ID every 10 seconds. I am using document.getElementById() to find the right cell. However, I have noticed that...
29
by: Nick | last post by:
I've seen a few frameworks use the following: function $(id) { return document.getElementById(id); } Then to use: $('something').innerHTML = 'blah'; I'm just trying to roll this out to my...
8
by: cyqotiq | last post by:
First, let me state that this is not necessarily a Firefox problem, as I haven't fully tested in IE just yet. Second, let me state that this is not the typical "getElementById not working Firefox"...
3
by: GarryJones | last post by:
The following works in MSIE but not firefox. I suspect it has something to do with the fact that the element I am trying to access is not the "tid" which is the name of the DIV that is passed to...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.