471,888 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,888 software developers and data experts.

JS error, "x has no properties" - accordion-style dropdown menu

I have an accordion style dropdown list/sublist menu (functions similar to the "today on WebMD video" widget found on http://www.webmd.com/) - it will allow users to click on a headline (from the main list) to open up the full listing (sublist) below that headline.

My JavaScript is almost but not quite working as needed. It also serves up an error that an object "has no properties". The particular error message and line of code it refers to:

Expand|Select|Wrap|Line Numbers
  1. sf has no properties
  2.  var state = sf.style.display;
I'll explain further and give actual code snippets - hopefully somebody can see something obvious that I need to fix?

Basically, I'm coding a tool that has a list of headlines (coded in definition list, DL). Clicking on a headline will reveal all the items listed below it in a subordinate list (coded as a unordered list, UL, nested inside a DD tag). Click on the same headline again, and it closes. Or, click on any other headline and the previously opened headline closes.

Another action also requiring JS is the position of the background image used on the top-level headlines (in the main list) needs to change when their respective subordinate ULs are expanded. Once the subordinate is closed, the background image also needs to change back to it's original position.

It is actually very similar to a basic drop-down list with rollovers, but because this requires closing/opening (i.e. "accordion") of the top level items via clicking events (and not mouseovers / hover events), it can't be done with only CSS and requires Javascript.

OK, hopefully that clearly explains the goal. What I've got so far is basic HTML and CSS - truncated version below:

<dt id="featured1" onclick="toggleDisplay('subfeat1');toggleBgPos('fe atured1');return false;">Headline 1</dt>
<dd id="subfeat1">
<li><a href="#" title="link info">1.1 Lorem ipsum</a></li>
<li><a href="#" title="link info">1.2 Dolor sit amet</a></li>
<dt id="featured2" onclick="toggleDisplay('subfeat2');toggleBgPos('fe atured2');return false;">Headline 2</dt>
<dd id="subfeat2">
<li><a href="#" title="link info">2.1 Lorem ipsum</a></li>
<li><a href="#" title="link info">2.2 Dolor sit amet</a></li>
<dt id="featured3" onclick="toggleDisplay('subfeat3');toggleBgPos('fe atured3');return false;">Headline 3</dt>
<dd id="subfeat3">
<li><a href="#" title="link info">3.1 Lorem ipsum</a></li>
<li><a href="#" title="link info">3.2 Dolor sit amet</a></li>

The simplified CSS:
Expand|Select|Wrap|Line Numbers
  1. dt {background: url(image.gif) 0 0 no-repeat;}
That is a "pixy rollover" style image - so the desired effects is that when a main list item (DT element) is clicked and it's subordinate is expanded, that background position will change to "0 -25px".

All the HTML/CSS validates. The problem is definitely (well, best I can tell) with the Javascript - as follows:

Expand|Select|Wrap|Line Numbers
  1. /* TOGGLE DISPLAY - show / hide the list */
  2. window.onload=toggleDisplay; // will load if Javascript enabled, otherwise all menus will stay shown / accessible
  4. function toggleDisplay(subFeatID) {
  5.     var sf = document.getElementById(subFeatID);
  7.     for (var i = 1; i<=10; i++) { // menus are hidden when the document loads up, but only if Javascript is enabled.
  8.         if (document.getElementById('subfeat'+i)) {document.getElementById('subfeat'+i).style.display='none';}
  9.     }
  11.     var state = sf.style.display; // toggle display rule
  12.     if (state == 'block')
  13.         sf.style.display = 'none';
  14.     else if (state != 'block')
  15.         sf.style.display = 'block'; 
  16. }
  19. /* TOGGLE BACKGROUND - change background image position accordingly */
  20. function toggleBgPos(featID) {
  21.     var f = document.getElementById(featID);
  23.         for (var i = 1; i<=10; i++) {
  24.         if (document.getElementById('featured'+i)) {document.getElementById('featured'+i).style.backgroundPosition='0px 0px';}
  25.     }
  27.     pos = f.style.backgroundPosition;
  28.     if (pos == '0px -25px')
  29.         f.style.backgroundPosition = '0px 0px';
  30.     else if (pos != '0px -25px')
  31.         f.style.backgroundPosition = '0px -25px'; 
  32. }
As is, the headlines open and close properly *except* in the case where you click on the open headline itself. If you click on any headline (DT element), it expands to show the sub list. Click on *any other* headline DT element, and the first one closes and the new one just clicked on expands. The background image position changes correctly.

So the only two problems are:
1. the error message
2. can't get the headline DT elements to close when clicking on the one that is open.

I EXTREMELY appreciate anybody who has read through all this and feels inspired enough to suggest some fix. Any ideas are welcome!

Oct 6 '07 #1
1 2523
5,821 Expert 4TB
Heya, Luispunchy. Welcome to TSDN!

When this line gets executed:
Expand|Select|Wrap|Line Numbers
  1. var sf = document.getElementById(subFeatID);
What is the value of subFeatID, and does an element exist at that point with that ID?
Oct 6 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Mike Hofer | last post: by
1 post views Thread by =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post: by
17 posts views Thread by David C. Ullrich | last post: by

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.