aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes document.getElementById( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "document.getElementById("id").value is not supported in firefox" Watch "document.getElementById("id").value is not supported in firefox" New topic
Author

document.getElementById("id").value is not supported in firefox

Aalok Mishra
Ranch Hand

Joined: Dec 23, 2008
Posts: 41
Hi All,

I have written a javascript function which runs fine in IE but when it comes down to Firefox it just craps evrything up. The function is not working at all. When I tried to find the reason I found that document.getElementById("id").value is not getting populated. Can anyone help me in it?
Here is the code:


I am sorry but I can not post entire code in here for security reasons. Please have a look at this snippet and let me know what is wrong in there.

Thanks in Advance


Thanks and Regards,
Aalok Mishra
InfoCepts, LLC
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Do you actually have an id="txtSearch" on your element?

Eric
Aalok Mishra
Ranch Hand

Joined: Dec 23, 2008
Posts: 41
Hi Eric,

Yes I actually have id as txtSearch. Actually this is the id of the text box which the user will be using to input search string.The id is just for convience and the same code is working just fine in IE6 and above.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Have you used firebug [www.getFirebug.com] to add break points and see what is happening? Are you cached in Firefox with some old version? [Ctrl-R]

Eric
Aalok Mishra
Ranch Hand

Joined: Dec 23, 2008
Posts: 41
Hi Eric,

yes I have used FireBug but of no use as such. It is saying that the object is null. The value is not getting supported and the object is displayed as null for this.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
does alert(v1); say it is an object?

Can you write a dummy page with just the basics and recreate the error?

Without more info there is not much that I can do to help you.

Eric
Aalok Mishra
Ranch Hand

Joined: Dec 23, 2008
Posts: 41
Hi Eric,

This is the message what fireBug gave to me when I tried to run that function:

document.getElementById("txtSearch") is null
displayAddResults()lookup.js (line 278)
onclick(click clientX=874, clientY=93)lookupMa...yRg%3D%3D (line 2)
[Break on this error] var v2 = document.getElementById("txtSearch").value;

The alert is displaying null
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
if the statement alert(document.getElementById("txtSearch")) gives you null, than that is telling me that you DO NOT have an element with an id="txtSearch" listed on the page or you are calling it before it is rendered to the page.

Reason why IE might be working is that IE says if it does not have an id of "txtSearch", it looks for a name="txtSearch", which is flat out wrong.

Eric
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
Would it be possible to post just the <input> tag declaration in your html. If it's created via script of some kind view the source after it's loaded and show the input tag then?
Aalok Mishra
Ranch Hand

Joined: Dec 23, 2008
Posts: 41
Hi Eric,

The id is there. I have used struts tags to define that text box. Here is the code:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Showing Struts source in an HTML forum is useless. How is anyone supposed to know what the Struts tags generated? SHow the generated HTML.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Aalok Mishra
Ranch Hand

Joined: Dec 23, 2008
Posts: 41
Here is the generated Html for you:

Aparna Raghavan
Greenhorn

Joined: Dec 07, 2008
Posts: 9
if you see the html code, the name is txtSearch and not the id.. thats why it is working in IE and not in firefox... as told , if IE does not find the id it will look for name. firefox doesn't.
Aalok Mishra
Ranch Hand

Joined: Dec 23, 2008
Posts: 41
Does that mean that I need to change all the getElementById in to getElementsByName?
But this is gonna create more trouble I guess

Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6


Eric
Aalok Mishra
Ranch Hand

Joined: Dec 23, 2008
Posts: 41
Just one question Eric..
What does this mean??


Eric Pascarello Today 5:26:09 PM Subject: document.getElementById("id").value is not supported in firefox

--------------------------------------------------------------------------------

view plaincopy to clipboardprint?
document.forms[0].elementName.value = "I like cheese";
document.forms[0].elementName.value = "I like cheese";


Eric


Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
That is how you reference an element in a form with its name.




Eric
Aalok Mishra
Ranch Hand

Joined: Dec 23, 2008
Posts: 41
Hi Eric,

thanks for the tip.

Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
Just beware that name is non-unique whereas id is. Name will get submitted with the value of your input when you post. General use is for checkboxes and radio buttons so they may share the same name with different values.

Edit: also, if I recall there is an attribute when you create the struts tag called style-id that creates the DOM id as you would with regular HTML. You maybe able to use that instead of name.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: document.getElementById("id").value is not supported in firefox