473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 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;
  3.  
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:

[HTML]
<dl>
<dt id="featured1" onclick="toggleDisplay('subfeat1');toggleBgPos('fe atured1');return false;">Headline 1</dt>
<dd id="subfeat1">
<ul>
<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>
</ul>
</dd>
<dt id="featured2" onclick="toggleDisplay('subfeat2');toggleBgPos('fe atured2');return false;">Headline 2</dt>
<dd id="subfeat2">
<ul>
<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>
</ul>
</dd>
<dt id="featured3" onclick="toggleDisplay('subfeat3');toggleBgPos('fe atured3');return false;">Headline 3</dt>
<dd id="subfeat3">
<ul>
<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>
</ul>
</dd>
</dl>
[/HTML]

The simplified CSS:
Expand|Select|Wrap|Line Numbers
  1. dt {background: url(image.gif) 0 0 no-repeat;}
  2.  
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
  3.  
  4. function toggleDisplay(subFeatID) {
  5.     var sf = document.getElementById(subFeatID);
  6.  
  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.     }
  10.  
  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. }
  17.  
  18.  
  19. /* TOGGLE BACKGROUND - change background image position accordingly */
  20. function toggleBgPos(featID) {
  21.     var f = document.getElementById(featID);
  22.  
  23.         for (var i = 1; i<=10; i++) {
  24.         if (document.getElementById('featured'+i)) {document.getElementById('featured'+i).style.backgroundPosition='0px 0px';}
  25.     }
  26.  
  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. }
  33.  
  34.  
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!

Thanks!
Oct 6 '07 #1
1 2637
pbmods
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);
  2.  
What is the value of subFeatID, and does an element exist at that point with that ID?
Oct 6 '07 #2

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

Similar topics

1
by: David | last post by:
Lately I have been getting the following error with all of my web applications, in debug and release mode. "Error while trying to run project: Unable to start debugging on the web server. Could...
3
by: Brian Foree | last post by:
I am developing an ASP.NET application that uses Access 2000 as its backend, and have just started getting the following error on 2 ASP.NET pages that had been working until late last week (and I...
1
by: gerr | last post by:
Visual studio 2003 "HTTP/1.1 500 Internal Server Error" when trying to create a new Asp.net site with Visual Studio 2003 on localServer/IIS 5.1. Anyone have any new solutions or urls to point me...
1
by: gerr | last post by:
Visual studio 2003 "HTTP/1.1 500 Internal Server Error" when trying to create a new Asp.net site with Visual Studio 2003 on localServer XP Pro/IIS 5.1. Everything worked fine prior to installing...
4
by: Bill Coan | last post by:
NOTE: This was posted earlier to vsnet.vstools.office under a different subject line but received no response. I'm having a problem automating Word's Find object from a .NET application, using...
1
by: dasayu | last post by:
Hi, I have a custom object called gridWidget. I am consistantly getting an error in FireFox when I click on an href, which calls a function defined on the object. The generated link looks similar...
2
by: hardrock | last post by:
Hello! I'm working with the prototype library version 1.4.0 and having a strange error lately. When I want to make an Ajax.Updater call, it basically works. But as soon as I put the call into...
2
by: Mike Hofer | last post by:
In my ASP.NET application, *one* page would not render in the application. All others would show up just fine, but when the user clicked the button to browse to this one page, I'd get a nasty error...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.