sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
GarryJones's Avatar

getElementById ignored by Firefox


Question posted by: GarryJones (Guest) on September 5th, 2008 10:05 PM
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 both
cases any entry is left standing and the focus is not passed in
firefox, whilst both work fine in MSIE.

First attempt (with getElementById)

function InsertContentshowgruppanm(tid) {
if( document.getElementById(tid).style.display == "none") {
document.getElementById(tid).style.display = "block";
document.getElementById("grant_ask").selectedIndex=0;
document.getElementById("tavfnmkas").value="";
document.getElementById("tavfnmkas").focus()
}

else {
document.getElementById(tid).style.display = "none";
}
}


Second attempt (with name of form inserted)

function InsertContentshowgruppanm(tid) {
if( document.getElementById(tid).style.display == "none") {
document.getElementById(tid).style.display = "block";
document.getElementById("grant_ask").selectedIndex=0;
document.prel_svlmst_grpcol.tavfnmkas.focus()
}
else {
document.getElementById(tid).style.display = "none";
}
}


So obvious question. Whats the syntax for firefox?



3 Answers Posted
Gregor Kofler's Avatar
Guest - n/a Posts
#2: Re: getElementById ignored by Firefox

GarryJones meinte:
Quote:
Originally Posted 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.


[snip]

DIVs don't have names. Forms and their elements have.

Anyway, your "problem" has been asked and answered hundreds of times
before. MSIE automatically (and wrongly) populates the id property of
elements with the same value of the name property, hence you can access
form elements with names via gEBI() - but only in IE. gEBI() needs
explicitly set (unique) ids. Otherwise resort to getElementsByName() -
note the plural.

Gregor



--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
GarryJones's Avatar
Guest - n/a Posts
#3: Re: getElementById ignored by Firefox

On 5 Sep, 23:13, Gregor Kofler <use...@gregorkofler.atwrote:
Quote:
Originally Posted by
GarryJones meinte:
>
Quote:
Originally Posted by
The following works in MSIE but not firefox.

DIVs don't have names. Forms and their elements have.


Still no go I am afraid....

Let me explain. The DIV has an ID and the first 2 rows of code either
display or hide the DIV which is correctly called by its id. Now, in
that DIV is a form. And on that form there are some elements which
have names. When the user clicks the link which fires this code it is
supposed to empty any previously entered value.

What I want is for the form to start with focus in the tavfnmkas
field. This field should be empty so any left over data from previous
viewings should be cleaned out when the button is clicked and the DIV
(with the form) toggles status "none" to status "block" and becomes
visible.

What these three "document.getElementsByName" lines should do is

document.getElementsByName("grant_ask").selectedIndex=0; //SHOULD set
the selected option index to zero in a SELECT
document.getElementsByName("tavfnmkas").value=""; //SHOULD removed any
previously entered data from the field tavfnmkas
document.getElementsByName("tavfnmkas").focus() //SHOULD place focus
in the field tavfnmkas

"None of the above" is the result of my competence and understanding
of this. I am very gratefull of any further help anyone would like to
provide.

My latest attempt using getElementsByName
***********************************************
function InsertContentshowgruppanm(tid) {
if( document.getElementById(tid).style.display == "none") {
document.getElementById(tid).style.display = "block";
document.getElementsByName("grant_ask").selectedIndex=0;
document.getElementsByName("tavfnmkas").value="";
document.getElementsByName("tavfnmkas").focus()
}

else {
document.getElementById(tid).style.display = "none";
}
}

Any help appreciated!

Garry Jones
Sweden
Gregor Kofler's Avatar
Guest - n/a Posts
#4: Re: getElementById ignored by Firefox

GarryJones meinte:
Quote:
Originally Posted by
document.getElementsByName("grant_ask").selectedIndex=0;
document.getElementsByName("tavfnmkas").value="";
document.getElementsByName("tavfnmkas").focus()


getElementsByName() returns a collection (didn't I mention the plural?),
therefore you need document.getElementsByName("...")[0].propertyOrMethod.

Gregor


--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
 
Not the answer you were looking for? Post your question . . .
197,013 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,013 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors