firefox and 'element.style.display'

B

basanistes

Hi,

I have tried several things to get firefox show a bullet list (<ul> when I
click on the list

having
1) <div onclick="javascript....">
2) <a href="javascript:..."
(see full HTML below!)

but in both cases, the swapP function is not triggered.
Why does firefox not bother about this?


<script language="javascript" type="text/javascript">
function swapP(obj)
{var testit = obj.style.display;
if ( testit == undefined) return false;
obj.style.display = testit == 'block' ? 'none' : 'block';
}
</script>




(snippet)

<li>
<a id="_ctl0_content_faqList__ctl1_issue"
href="javascript:swapP(_ctl0_content_faqList__ctl1_answer);">
blah question blah blah ?
</a>

<div id="_ctl0_content_faqList__ctl1_answer" class="answer">
blha blah blah answer
</div>
</li>

(class answer is inside a css where div.answer {display=none;}
 
D

David Dorward

basanistes said:
but in both cases, the swapP function is not triggered.
Why does firefox not bother about this?
<a id="_ctl0_content_faqList__ctl1_issue"
href="javascript:swapP(_ctl0_content_faqList__ctl1_answer);">

Since the JavaScript variable "_ctl0_content_faqList__ctl1_issue" is not
defined.

There are two reasons for this:

1: The first character of an id must NOT be an underscore.

http://www.w3.org/TR/html4/types.html#type-name

2: Firefox does not automatically create, for each element with an id, a
variable in the global scope named after that id and containing a reference
to the element.

http://www.mozilla.org/docs/web-developer/upgrade_2.html#dom
 
R

Randy Webb

basanistes said the following on 7/28/2006 1:18 PM:
Hi,

I have tried several things to get firefox show a bullet list (<ul> when
I click on the list

having
1) <div onclick="javascript....">
2) <a href="javascript:..."
(see full HTML below!)

but in both cases, the swapP function is not triggered.
Why does firefox not bother about this?

It does, but view the error console.
<script language="javascript" type="text/javascript">
function swapP(obj)
{var testit = obj.style.display;

var testit=document.getElementById(obj).style.display

As the "obj" you pass is not the object, but rather the ID of the
object. Well, except in IE.
<a id="_ctl0_content_faqList__ctl1_issue"
href="javascript:swapP(_ctl0_content_faqList__ctl1_answer);">

First problem is the id of the object is not quoted in the function call.
Second problem is that IE is only browser with the ignorance built in to
create a global reference to any object with an ID.
Third problem is the use of the javascript: pseudo-protocol.
 
B

basanistes

David Dorward said:
Since the JavaScript variable "_ctl0_content_faqList__ctl1_issue" is not
defined.

As I told that I did not include the whole page. Of course, you can assume
that I''m not that 'stupid' :)
There are two reasons for this:

1: The first character of an id must NOT be an underscore.

that must have been the reason then.
 
B

basanistes

Randy Webb said:
basanistes said the following on 7/28/2006 1:18 PM:

It does, but view the error console.


var testit=document.getElementById(obj).style.display

that did the trick.
Thanks!
As the "obj" you pass is not the object, but rather the ID of the object.
Well, except in IE.

This sounds ok. THe object is referenced by id.
First problem is the id of the object is not quoted in the function call.
Second problem is that IE is only browser with the ignorance built in to
create a global reference to any object with an ID.
Third problem is the use of the javascript: pseudo-protocol.

So you mean that <div onclick=""> etc should be better?
 
R

Randy Webb

basanistes said the following on 7/28/2006 2:23 PM:
So you mean that <div onclick=""> etc should be better?

<button onclick="">
<a href="noscript.html" onclick="something();return false">

Anything is better than href="javascript:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top