473,385 Members | 1,757 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,385 software developers and data experts.

why does document.getElementById(optionDivId) not find id of element that is clearly there


Imagine I have these two lines of HTML:

<p>Pick a list: (<a href="mcControlPanel.php"
onClick="hideOrShowDivById('newMailList'); return false;">Create New
List?</a>)</p>

<form id="newMailList" method="post" action="mcControlPanel.php">
Imagine I have this function:
function hideOrShowDivById(optionDivId) {
if (document.getElementById(optionDivId)) {
var optionDiv = document.getElementById(optionDivId);
if (optionDiv.style.display == 'block') {
optionDiv.style.display='none';
} else {
optionDiv.style.display='block';
}
} else {
alert("There was no item on the page called " + optionDivId);
}
}

Why would I get the error message ""There was no item on the page
called newMailList"

May 23 '06 #1
2 1543
Jake Barnes wrote:
Imagine I have these two lines of HTML:

<p>Pick a list: (<a href="mcControlPanel.php"
onClick="hideOrShowDivById('newMailList'); return false;">Create New
List?</a>)</p>

<form id="newMailList" method="post" action="mcControlPanel.php">
Imagine I have this function:
function hideOrShowDivById(optionDivId) { if (document.getElementById(optionDivId)) { var optionDiv = document.getElementById(optionDivId);
if (optionDiv.style.display == 'block') optionDiv.style.display='none'; else optionDiv.style.display='block';
}
else
alert("There was no item on the page called " + optionDivId); }
Why would I get the error message ""There was no item on the page
called newMailList"


Have you checked the integrity of your HTML?
Ensure that all applicable elements have closing tags.

May 23 '06 #2
Jake Barnes wrote:
Imagine I have these two lines of HTML:

<p>Pick a list: (<a href="mcControlPanel.php"
onClick="hideOrShowDivById('newMailList'); return false;">Create New
List?</a>)</p>

<form id="newMailList" method="post" action="mcControlPanel.php">

Imagine I have this function:

function hideOrShowDivById(optionDivId) {
if (document.getElementById(optionDivId)) {
var optionDiv = document.getElementById(optionDivId);
if (optionDiv.style.display == 'block') {
In this example, optionDiv is a reference to a form element. Most
elements in the page inherit their display characteristics from a
default stylesheet, they are not contained in the element's style object.

This is easily fixed by modifying your script to:

if (optionDiv.style.display == '') {
optionDiv.style.display = 'none';
} else {
optionDiv.style.display = '';
}
or more concisely and with feature detection:

var o = optionDiv.style;
if (o){
o.display = ('' == o.display)? 'none' : '';
}

Now it will work with a wide variety of element types, regardless of
their default display attribute value.

There are many different values for the display attribute, hence it is
recommended to switch between '' and 'none' rather than some specific
value like 'block' or 'inline'. Setting it to '' allows it to return to
the default (or whatever it might have been set to by CSS).

<URL:http://www.w3.org/TR/CSS21/visuren.html#propdef-display>

[...]
Why would I get the error message ""There was no item on the page
called newMailList"


Because the call to optionDiv.style.display returns an empty string
(''), so the test:

if (optionDiv.style.display == 'block')

is always false.
--
Rob
Group FAQ: <URL:http://www.jibbering.com/faq/>
May 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tor Hovland | last post by:
I'm trying to transform the document element of incoming xml files, however, I'm having trouble with namespace references not appearing correctly. Here's an example input file: <?xml...
1
by: Andy Fish | last post by:
Hi, I'm using XML DOM in Java and I have a function like this Element makeSomeXml(Document doc) {..} This returns an Element owned by (but not appended to) the specified document. Now,...
9
by: s_m_b | last post by:
I'm trying to get an <a> element to gain the focus onload, but only get back 'has no properties'. Reading through this ng, its clear that unless the element is within a form, this doesn't happen,...
10
by: b.dam | last post by:
I'm trying the following: function grid() { this._el = document.createElement("TABLE"); var self = this; document.addEventListener("onkeydown", function(event) {self.gridKeyDown(event);},...
3
by: Franck | last post by:
Hi, got a problem with my javascript and I think it's more linked to my asp.net code than script. (the reason I post here) In an ascx control, I've got two controls, one textbox and one Telerik...
17
by: pbd22 | last post by:
hi. i keep getting this error. as i understand it, my xml isn't formatted correctly. the online errata suggests the standard formatting to solve this problem: element (tab) (tab) element
4
by: NewToCPP | last post by:
I checked the microsoft MSDN and it says the following: "find returns an iterator that addresses the location of an element with a specified key, or the location succeeding the last element in...
2
by: pbd22 | last post by:
Hi. I have an event handler in VB.NET that gets called several times. The output of the event handler is XML that is being called by an xmlhttp request from the client. I am using a...
4
by: Miroslav Stampar [MCSD.NET / Security+] | last post by:
I need to get all links from an document element. In sample below how can i get link to "www.google.com" from element with ID="here" <html> <head> <title></title>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.