473,692 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why does document.getEle mentById(option DivId) not find id of element that is clearly there


Imagine I have these two lines of HTML:

<p>Pick a list: (<a href="mcControl Panel.php"
onClick="hideOr ShowDivById('ne wMailList'); return false;">Create New
List?</a>)</p>

<form id="newMailList " method="post" action="mcContr olPanel.php">
Imagine I have this function:
function hideOrShowDivBy Id(optionDivId) {
if (document.getEl ementById(optio nDivId)) {
var optionDiv = document.getEle mentById(option DivId);
if (optionDiv.styl e.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 1563
Jake Barnes wrote:
Imagine I have these two lines of HTML:

<p>Pick a list: (<a href="mcControl Panel.php"
onClick="hideOr ShowDivById('ne wMailList'); return false;">Create New
List?</a>)</p>

<form id="newMailList " method="post" action="mcContr olPanel.php">
Imagine I have this function:
function hideOrShowDivBy Id(optionDivId) { if (document.getEl ementById(optio nDivId)) { var optionDiv = document.getEle mentById(option DivId);
if (optionDiv.styl e.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="mcControl Panel.php"
onClick="hideOr ShowDivById('ne wMailList'); return false;">Create New
List?</a>)</p>

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

Imagine I have this function:

function hideOrShowDivBy Id(optionDivId) {
if (document.getEl ementById(optio nDivId)) {
var optionDiv = document.getEle mentById(option DivId);
if (optionDiv.styl e.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.styl e.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#pr opdef-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.styl e.display == 'block')

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

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

Similar topics

0
1581
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 version="1.0" encoding="utf-8" ?> <inputdoc xmlns:a="http://www.dummy.org" something="true"> <hello somethingelse="false" /> <foo> <!-- a comment --> <bar />
1
2612
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, when I call this function, sometimes I want to append the result
9
9502
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, but according to the w3 TR on html 4.01, the a element can take onFocus commands, which to me means you can do document.getElementById(element).focus() ? I'm trying to get the focus to a specific paragraph, so the page scrolls to it on loading,...
10
9161
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);}, false); }
3
2765
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 TreeView. When I try document.getElementById('<%= MyTextBox.ClientID %>').innerText= 'X';
17
3920
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
4314
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 the map if no match is found for the key." What does it mean by the location succeeding the last element.... how do we know that the element not found in the MAP?
2
17392
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 stringbuilder to create the XML document by appending the various parts: sbhtml.Append("<Some XML Tag>")
4
23816
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
8600
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9079
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8794
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5817
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4322
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2234
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1955
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.