473,837 Members | 1,494 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 1912
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
2698
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
9913
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
9281
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
14379
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
4364
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
19316
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
3147
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
2067
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...
1
10630
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
10275
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
9407
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
7811
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
7005
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5670
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4476
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3126
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.