473,657 Members | 2,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>Thi s is more
text!</PRE>">
<SCRIPT LANGUAGE="javas cript">
var m = (document.getEl ementById &&
document.getEle mentById('moreT ext'))
? document.getEle mentById('moreT ext').value
: '<PRE>no more text</PRE>';
document.writel n(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 1889
Dave Hammond wrote on 15 feb 2005 in comp.lang.javas cript:
<SCRIPT LANGUAGE="javas cript">
var m = (document.getEl ementById &&
document.getEle mentById('moreT ext'))
? document.getEle mentById('moreT ext').value
: '<PRE>no more text</PRE>';
document.writel n(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>Thi s is more
text!</PRE>">
<SCRIPT LANGUAGE="javas cript">
var m = (document.getEl ementById &&
document.getEle mentById('moreT ext'))
? document.getEle mentById('moreT ext').value
: '<PRE>no more text</PRE>';
document.writel n(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.getEle mentById('moreT ext') ~~~~ [...]


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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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>Thi s is
more
text!</PRE>">
<SCRIPT LANGUAGE="javas cript">
var m = (document.getEl ementById &&
document.getEle mentById('moreT ext'))
? document.getEle mentById('moreT ext').value
: '<PRE>no more text</PRE>';
document.writel n(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>Thi s is
more
text!</PRE>">
<SCRIPT LANGUAGE="javas cript">
var m = (document.getEl ementById &&
document.getEle mentById('moreT ext'))
? document.getEle mentById('moreT ext').value
: '<PRE>no more text</PRE>';
document.writel n(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.getEle mentById

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.javas cript 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>Thi s is
more
text!</PRE>">
<SCRIPT LANGUAGE="javas cript">
var m = (document.getEl ementById &&
document.getEle mentById('moreT ext'))
? document.getEle mentById('moreT ext').value
: '<PRE>no more text</PRE>';
document.writel n(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.getEle mentById

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.javas cript 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2675
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 function with either MS IE 6.0 or Mozilla Firefox 0.9. Comments below indicate that if I hardcode an element ID I can avoid the error, but I obviously want to parameterize it. Here are some snippets of my code: -----snippet...
5
440
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
9875
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 id="MyTest" style="position:absolute;top:0px;left:0;height:12;width:12;text-align:center">'+MyString+'</div>'); Now if MyString contains 6 characters, I end up with 6 <div id="MyTest"> containers. With IE I am able to access these containers as...
3
9257
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 work on FireFox or Netscape. What could be wrong? The function: function setActiveTab(tabNo) {
3
14365
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 < userGroupArray.length; i++){ for (var j = 0; j < favElement.length; j++){ if(favElement.options.value == userGroupArray){
19
4341
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 document.getElementById() in IE is freezing the browser for about 1 second unlike Firefox. Can anyone suggest any speed improvements for painting data into a large number of cells to speed up the process?
29
19231
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 site and so far doing this has saved about 8KB of javascript (lots of ajax/dynamic elements). I just
8
3134
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" post. Third, there are pieces of this code that I am not at liberty to display, change, discuss, or re-implement. As far as this question is concerned, this means that (1) I cannot use 3rd party libraries, which is why I've implemented my own...
3
2055
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 this javascript. The function is a "show/hide" for a form. I want to ensure that any possible previously entered value is cleared so the user starts with an empty field with focus. I have tried to ways, with naming and getElementById but in...
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.