By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,199 Members | 1,458 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,199 IT Pros & Developers. It's quick & easy.

id and problem in Firefox

P: n/a
In IE, I could be able to directly refer the "id", but it isn't
possible in Firefox. Somewhere I read the solution is to refer the id
like document.getElementById("month") in Firefox. If I do so, the
script works well in Firefox, but IE throws error. (I have added the
code snippet below).

So, my question is: is there anyway to make the script work in all
browser *without* any browser fix ie, without adding any browser
detection check? Is this behavior is only for Firefox or any other
browsers also work like this (I don't have access to many browsers to
test)? TIA

<snippet>

<HEAD>
<SCRIPT LANGUAGE=javascript>
<!--
function PopulateMonths()
{
//month is id used with select
//month = document.getElementById("month"); //<-- for Firefox
month.options.length = 0; //clear options
for(var i=1; i<=12; ++i)
{
opt = new Option();
opt.text = i;
opt.value = i;
month.options[i-1] = opt;
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<!--month options-->
<SELECT name="month" id="month">
</SELECT>
<!--calling script-->
<SCRIPT>
PopulateMonths(); //calling populate for the above options
</SCRIPT>
</BODY>

</snippet>

--
Email: rrjanbiah-at-Y!com
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
R. Rajesh Jeba Anbiah wrote:
In IE, I could be able to directly refer the "id", but it isn't
possible in Firefox. Somewhere I read the solution is to refer the id
like document.getElementById("month") in Firefox. If I do so, the
script works well in Firefox, but IE throws error. (I have added the
code snippet below).
<snip> <HEAD>
<SCRIPT LANGUAGE=javascript>
<!--
function PopulateMonths()
{
//month is id used with select
//month = document.getElementById("month"); //<-- for Firefox
month.options.length = 0; //clear options
for(var i=1; i<=12; ++i)
{
opt = new Option();
opt.text = i;
opt.value = i;
month.options[i-1] = opt;
}
} <snip> <SELECT name="month" id="month">
</SELECT>
<!--calling script-->
<SCRIPT>
PopulateMonths(); //calling populate for the above options
</SCRIPT>


Odd, similar works OK for me across both IE and Firefox. Maybe IE is getting
upset because your assigning the widget to a variable with the same name as
an HTML Element in the same DOM scope? Try enclosing the select in
<form>...</form> or change the JS variable name.

HTH

C.
Jul 23 '05 #2

P: n/a
R. Rajesh Jeba Anbiah wrote:
In IE, I could be able to directly refer the "id", but it isn't
possible in Firefox. Somewhere I read the solution is to refer the id
like document.getElementById("month") in Firefox. If I do so, the
script works well in Firefox, but IE throws error. (I have added the
code snippet below). [snip]

<snippet>

<HEAD>
<SCRIPT LANGUAGE=javascript>
<!--
function PopulateMonths()
{
//month is id used with select
//month = document.getElementById("month"); //<-- for Firefox
month.options.length = 0; //clear options
for(var i=1; i<=12; ++i)

[snip]

I have no problem using code like yours in MSIE - perhaps you have an
older version of IE where the problem arises? Certainly in modern
versions, the W3C standard DOM and object methods like getElementById
sholud work perfectly well.

Don't bother to "detect browsers" though - check the objects and methods
themselves so you can do something like:-

if (document.getElementById) month = document.getElementById ("month")
else {
if (document.all && <your MSIE object/method>) {
// do MSIE code here
}
}
Jul 23 '05 #3

P: n/a
> <SELECT name="month" id="month">

Also, I'd make these variables different.

Robert
Jul 23 '05 #4

P: n/a
DU
R. Rajesh Jeba Anbiah wrote:
In IE, I could be able to directly refer the "id", but it isn't
possible in Firefox. Somewhere I read the solution is to refer the id
like document.getElementById("month") in Firefox. If I do so, the
script works well in Firefox, but IE throws error. (I have added the
code snippet below).

So, my question is: is there anyway to make the script work in all
browser *without* any browser fix ie, without adding any browser
detection check? Is this behavior is only for Firefox or any other
browsers also work like this (I don't have access to many browsers to
test)? TIA

<snippet>

<HEAD>
<SCRIPT LANGUAGE=javascript>
<script type="text/javascript">
<!--
function PopulateMonths()
{
//month is id used with select
But you also use name="month" in your code. So, there is a confusion
already here.
//month = document.getElementById("month"); //<-- for Firefox
month.options.length = 0; //clear options
for(var i=1; i<=12; ++i)
{
opt = new Option();
opt.text = i;
opt.value = i;
month.options[i-1] = opt;
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<!--month options-->
You need to insert <form action=""> element here.
<SELECT name="month" id="month">
</SELECT>
<!--calling script-->
<SCRIPT>
PopulateMonths(); //calling populate for the above options
</SCRIPT>
</BODY>

</snippet>


I would validate your HTML markup code first of all, preferably with a
strict doctype decl. because that will trigger MSIE 6 into web standards
compliant rendering mode.

List of valid DTDs you can use in your document.
http://www.w3.org/QA/2002/04/valid-dtd-list.html

Like someone else in the thread said, you first need to embed that
<select> inside a <form>. <form> also requires an action attribute.

Your populate select function could be streamlined a bit and clarified also:

function PopulateMonths()
{
var objMonthSelect;
if(document.getElementById)
{
objMonthSelect = document.getElementById("month");
}
else if(document.all && !document.getElementById) // MSIE 4 only
{
objMonthSelect = month;
};
objMonthSelect.options.length = 0; //clear options
for(var i=0; i<12; i++)
{
opt = new Option();
opt.text = i;
opt.value = i;
objMonthSelect.options[i] = opt;
};
}

You should also always use differential (and preferably meaningful)
identifiers for name attribute and id attribute for several reasons:
code reviewing by others, better discrimination of how your code
accesses nodes, better understanding of your code inner logic. <select
name="month" id="month"> just confuses the way certain browser actually
accesses nodes when one does
month.[some attribute]

Since above 96% of all browsers in use out there support flawlessly
getElementById method, then there is no problem with replacing
month
by
document.getElementById("month")
with proper object support for that method

Finally, I recommend you always call functions like PopulateMonths() on
the onload event of the body element, not as the document is being
parsed and rendered for several reasons.

DU
Jul 23 '05 #5

P: n/a
Colin McKinnon <co**************@andthis.mms3.com> wrote in message
In IE, I could be able to directly refer the "id", but it isn't
possible in Firefox. Somewhere I read the solution is to refer the id
like document.getElementById("month") in Firefox. If I do so, the
script works well in Firefox, but IE throws error. (I have added the
code snippet below).
<snip> Odd, similar works OK for me across both IE and Firefox. Maybe IE is getting
upset because your assigning the widget to a variable with the same name as
an HTML Element in the same DOM scope? Try enclosing the select in
<form>...</form> or change the JS variable name.


Great! Your solutions work like a charm! Thanks for your time and help.

--
Email: rrjanbiah-at-Y!com
Jul 23 '05 #6

P: n/a
Mark Preston <us****@mpreston.demon.co.uk> wrote in message news:<ce*******************@news.demon.co.uk>...
<snip>

Many thanks to all the experts who answered in this thread. Your
solutions and answers are really helpful.
//month is id used with select
//month = document.getElementById("month"); //<-- for Firefox
month.options.length = 0; //clear options
for(var i=1; i<=12; ++i)

[snip]

I have no problem using code like yours in MSIE - perhaps you have an
older version of IE where the problem arises?


I'm using new versions of IE and Firefox. Colin McKinnon, Robert
and DU solutions are working great. Many thanks again.

--
Email: rrjanbiah-at-Y!com
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.